Java – it makes sense to override the methods in the interface

There is already an answer to this question: > why Java 6 overrides keyset(), entryset() and values() interface in sortedmap1

Even using @ override can override foo in interface B, but is there any situation? Nothing can be rewritten because these two methods must be abstract without any body So I don't think there's anything. It makes sense, right?

So why can I override it in the interface?

Solution

One case is when you want to update the Javadoc document to reflect the more specific contract in the sub interface method, as shown in 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 (set:

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

Sub interfaces can also add a default implementation starting with Java 8

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>