简体   繁体   中英

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 ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM