问题描述
众所周知,接口可以扩展Java中的接口。我有一个问题,如果接口B扩展接口A,B不需要实现A中定义的方法。但是在java.util包中,List接口扩展了Collection接口,并且它实现了Collection方法,这些方法也是只需要方法声明。
As we know, interfaces can extend interface in the Java. I have a question about this, if interface B extends interface A, B need not implement the methods defined in the A. But in the java.util package, the List interface extends Collection interface, and it implements the Collection method, these methods also just have the method declaration.
为什么会这样做,有没有更好的做法?在子接口中实现方法之间是否有任何区别?
Why does it do this, and it there a better practice? Does it make any difference between implementing the method in the sub interface or not?
推荐答案
除了提供/替换方法之外,覆盖方法方法实现,允许提供更具体的javadoc,并缩小返回类型。
Overriding a method, besides providing/replacing a method implementation, allows to provide a more specific javadoc, and to narrow the return type.
例如, Collection.iterator()
由下式指定:
而 List.iterator()
由
这篇关于Java接口扩展了java.util包中的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!