4

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

why I have to add public ? why without public it does not work ?

as the Net-Beans says : " attempting to assign weaker access privileges; was public "

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);
    }
}
Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23
Moawiya
  • 147
  • 1
  • 2
  • 11
  • Duplicate of http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private? – jhobbie Aug 06 '14 at 13:00
  • 1
    @jhobbie: don't think so. this is about the difference between the implicit visibility between class and interface, not the definition of the various levels of visibility. – njzk2 Aug 06 '14 at 13:05

3 Answers3

8

All methods in interfaces are public.

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

You cannot reduce the visibility of the public methods to package-private because it violates the interface.

Xabster
  • 3,710
  • 15
  • 21
  • Precisely. If you feel it helps, you can change all the methods in your interface to `public void` - this way it is clearer that the implementing methods should likewise be `public void`. – vikingsteve Aug 06 '14 at 13:03
  • 1
    which is somewhat silly. implicit visibility in interface and in class is not the same, it always bugged me. same goes (even worse) for fields that are implicitly `public static final` – njzk2 Aug 06 '14 at 13:04
  • 1
    @njzk2 It has annoyed me that I can't explicitly say that a method/class is package-private. I have to leave out the declaration. My OCD is tingling. – Xabster Aug 06 '14 at 13:14
3

Because in an interface, all methods are by default public, and in a class, the default visibility of methods is "friend" - seen in the same package.

You can't narrow down the visibility when implementing a method, that's the rule.

Eutherpy
  • 4,471
  • 7
  • 40
  • 64
0

lets suppose you have a interface Printable,which contains a method print().

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

now because the method is declared inside interface , compiler implicitly adds public abstract before the method. like this :- interface for compiler

now as the method is been declared public , so now when you extend the method you have to add public before the method.

I think this might help you.

vishwampandya
  • 1,067
  • 11
  • 11