karthik gorijavolu
karthik gorijavolu

Reputation: 860

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?

Upvotes: 3

Views: 245

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533492

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.

Upvotes: 10

user1168492
user1168492

Reputation:

The public access specifier indicates that the interface can be used by any class in any package. If you do not specify that your interface is public, then your interface will only be accessible to classes that are defined in the same package as the interface.

Upvotes: 0

Related Questions