Overriding methods in the java interface

I wrote an interface in Java. after that I tried to implement it by overriding as shown in the code. but I get an error that I have to add to the public before this method.

Why should I add a publication? why doesn’t it work without an audience?

as NetBeans says: "an attempt to assign weaker access rights was publicly available"

the code:

    package tryinginterface;
interface Bicycle {

    //  wheel revolutions per minute
    void changeCadence(int newValue);

    void changeGear(int newValue);

    void speedUp(int increment);

    void applyBrakes(int decrement);
}


class ACMEBicycle implements Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;
    @Override 
        void changeCadence(int newValue) {
         cadence = newValue;
    }
    @Override
    void changeGear(int newValue) {
         gear = newValue;
    }
    @Override
    void speedUp(int increment) {
         speed = speed + increment;   
    }
    @Override
    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }
    @Override
    void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}
+4
source share
3 answers

All methods in the interfaces are publicly available.

All methods in a class without a visibility modifier are private-package.

You cannot reduce the visibility of public methods for private-package, as this violates the interface.

+8
source

, - "" - .

, .

+3

, Printable, print().

interface Printable{
   int MIN=5;
   void print();
}

, , . : -

, public, , , public .

, .

0

All Articles