Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why an implemented interface method be declared as public?

I just started learning java when i came across interface, I saw the following code:

interface Callback {
   void callback(int param);
}

class Client implements Callback {
   public void callback(int p) {
   }
}

why is that an implemented interface method be declared as public?

like image 343
karthik gorijavolu Avatar asked Feb 16 '26 16:02

karthik gorijavolu


1 Answers

The default modifier for an interface method is public abstract

The default modifier for a class method is package-local. These are not the same, and you can't override a public method with a package local one. You can override an abstract method with a non-abstract one.

You have to make your class method public, even though you don't have to put this in the interface.

like image 66
Peter Lawrey Avatar answered Feb 18 '26 06:02

Peter Lawrey