java - 覆盖接口(interface)中的方法是否有意义

标签 java interface overriding extends

<分区>

例如,我有一个接口(interface) A 和 B。A 有一个名为 foo 的(抽象)方法。 B 扩展了 A。

即使使用@Override 也可以覆盖接口(interface) B 中的 foo,但是有什么情况是这样的吗?没有什么可以重写的,因为这两种方法都必须是抽象的并且没有主体。所以我想这在任何情况下都说不通,对吗?

那么为什么可以在接口(interface)中覆盖?

最佳答案

一种情况是当您想要更新 Javadoc 文档以反射(reflect)子接口(interface)方法中更具体的约定时,Collection#addAll(Collection) 就是这种情况。和 List#addAll(Collection) :

  • 集合#addAll(集合):

    Adds all of the elements in the specified collection to this collection (optional operation)...

  • 列表#addAll(集合:

    Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation)...

子接口(interface)还可以添加从 Java 8 开始的默认实现。

关于java - 覆盖接口(interface)中的方法是否有意义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32788943/

相关文章:

java - 使用ViewPager后TabItem图标消失

java - JButton 鼠标悬停在运动监听器上的小信息框(工具提示)

java - 如何从 Hashtable<Integer,Hashtable<String,Integer>> h 获取值

java - AspectJ - 无法覆盖切入点 - 参数类型不匹配

java - 为什么饥饿单例模式是线程安全的

c# - 无法转换异常类型的 COM 对象

java - 我完全不知道如何在我的java程序中使用keylistener

performance - EJB3 - Session Bean调用另一个bean接口(interface)的方法

maven命令行如何为单个命令指向特定的settings.xml?

c++ - 与 C++ 中的指针相关的静态类型类