[英]why an implemented interface method be declared as public?
I just started learning java when i came across interface, I saw the following code: 当我遇到界面时,我才开始学习Java,我看到了以下代码:
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
? 为什么将已实现的接口方法声明为
public
?
The default modifier for an interface method is public abstract
接口方法的默认修饰符是
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.
如果您未指定您的接口是公共的,那么该接口将只能由与该接口在同一包中定义的类访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.