覆盖接口中的方法是否有意义

Does it make sense to override a method in an interface

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

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

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

一种情况是当您想要更新 Javadoc 文档以在子接口的方法中反映更具体的约定时,Collection#addAll(Collection) and List#addAll(Collection):

就是这种情况
  • Collection#addAll(Collection):

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

  • List#addAll(Collection:

    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)...

一个子接口也可以添加一个默认实现开始 Java 8.

子类型可以施加更多条件,更改 return 类型,更改抛出类型。一个例子

interface AutoCloseable
    void close() throws Exception

interface Closeable extends AutoCloseable
    void close() throws IOException

(子类型也可以用方法签名的擦除版本覆盖...但这是老话了)

在java8中,子接口可以为抽象方法提供默认实现

interface DummyCloseable extends Closeable
{
    default void close()
    {
        // do nothing
    }