简体   繁体   English

由于所有接口方法都是公共的,这是否意味着接口方法的所有实现都必须是公共的

[英]since all interface method are public, does that mean all implementation of interface method must be public as well

since all interface method are public, does that mean all implementation of interface method must be public as well? 由于所有接口方法都是公共的,这是否意味着接口方法的所有实现都必须公开? I have read that subclass can only assign access level that is higher than the original method. 我已经读过,子类只能分配高于原始方法的访问级别。 I have tried this with some code and it does seem to be true, but I was just wondering where can I find documentation regarding to this? 我用一些代码尝试了这个,它看起来确实如此,但我只是想知道在哪里可以找到有关此的文档?

You can find the official documentation of that requirement in the Java Language Specification . 您可以在Java语言规范中找到该要求的官方文档。

Quoting from 8.4.8.3. 引自8.4.8.3。 Requirements in Overriding and Hiding : 覆盖和隐藏的要求

The access modifier (§6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, as follows: 覆盖或隐藏方法的访问修饰符(第6.6节)必须至少提供与重写或隐藏方法一样多的访问权限,如下所示:

If the overridden or hidden method is public, then the overriding or hiding method must be public; 如果被覆盖或隐藏的方法是公共的,则覆盖或隐藏方法必须是公共的; otherwise, a compile-time error occurs. 否则,发生编译时错误。

If the overridden or hidden method is protected, then the overriding or hiding method must be protected or public; 如果被覆盖或隐藏的方法受到保护,则覆盖或隐藏方法必须受到保护或公开; otherwise, a compile-time error occurs. 否则,发生编译时错误。

If the overridden or hidden method has package access, then the overriding or hiding method must not be private; 如果重写或隐藏方法具有包访问权限,则覆盖或隐藏方法不得为私有方法; otherwise, a compile-time error occurs. 否则,发生编译时错误。

As in the official Java Tutorial by Oracle: 与Oracle官方Java教程一样:

interfaces are a kind of contract all implementing classes have to stick to 接口是一种所有实现类必须坚持的契约

This means, the signature of a method, the return type and the access modifier are not allowed to be changed. 这意味着, 不准的方法,该返回类型和访问修饰符的签名被改变。

https://docs.oracle.com/javase/tutorial/java/IandI/index.html https://docs.oracle.com/javase/tutorial/java/IandI/index.html

Also if you try to tag an interface-method with the @Override annotation, the compiler will throw an error. 此外,如果您尝试使用@Override注释标记interface-method,编译器将抛出错误。

Interface s are created to be an interface for using classes that implement them. Interface s的创建是使用实现它们的类的接口

For example we implement a Comparable interface to be trusted that the class has compareTo method which can be used by other classes. 例如,我们实现Comparable接口被信任的是,类具有compareTo方法,其可以通过其它类使用

So it's not possible and I don't see a point in doing that. 所以这是不可能的,我没有看到这样做的重点。

I didn't found a part of documentation which exactly refers to your question but it can be understood from this part: 我没有找到准确引用您的问题的文档的一部分,但从这部分可以理解:

If the overridden or hidden method is public, then the overriding or hiding method must be public; 如果被覆盖或隐藏的方法是公共的,则覆盖或隐藏方法必须是公共的; otherwise, a compile-time error occurs. 否则,发生编译时错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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