简体   繁体   English

为什么将已实现的接口方法声明为public?

[英]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.

相关问题 在类中实现接口后,重写在接口中声明的方法的返回类型 - override the return type of method declared in the interface after its implemented in class 为什么将已实现的接口方法声明为“公共”? - Why should I declare implemented interface methods as “public”? 为什么在Annotation接口中声明了Object的非最终公共方法? - Why non-final public methods of Object declared in Annotation interface? 如果接口是用kotlin编写的,为什么必须实现接口中的具体方法 - Why concrete method in interface must be implemented if the interface is written in kotlin 为什么在连接接口而不是语句接口下声明createStatement方法? - Why createStatement method is declared under Connection Interface instead of Statement interface? 如何@Override 在类中实现的接口中声明的属性 - How to @Override an Attribute declared in an Interface implemented in a class 为什么OnClickListener接口没有实现并在setOnClickListener()方法中作为新的传递? - Why OnClickListener interface is not implemented and passed as new in setOnClickListener() method? 为什么接口的泛型方法可以在Java中实现为非泛型? - Why a generic method of an interface can be implemented as non-generic in Java? 为什么我不能使用已实现接口的静态方法? - Why can't I use from the static method of the implemented interface? 为什么功能接口不是由类实现的? - Why the functional interface is not implemented by the class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM