The interface NavigableSet<E> extends interface SortedSet<E> which declares the method
SortedSet<E> subSet(E fromElement, E toElement) .
However, when I look at NavigableSet’s API, I see that it has declared the same method (with exactly the same signature). I also see that it says it inherits some methods (first, last, comparable) from SortedSet.
Since by definition, interfaces only contain method signatures, why would any child interface redeclare the same method that the parent interface has already declared (and there’s no difference in semantics, in the API javadoc)?
Edit: Came across a relevant and nice discussion here –
In Java when one interface extends another, why would one redeclare a method in a subinterface?
Doing so is certainly allowed in Java.
Here is the source code:
Perhaps the author of the class found it worthwhile to repeat the definition because there is a second definition of
subSetthat returns aNavigableSet, so having both show up side-by-side in the Javadoc listing was nicer than having one in the main method listing and one in the “inherited” section.There was surely no good reason to do this for
firstandlast, so this was not done.Here is the rationale from the Javadocs for adding the second versions of subSet, headSet, and tailSet:
You might wish to try your own experiment, designing a couple of interfaces, one inheriting from another, with the subinterface adding a new version of an inherited method, to see if the respecification was necessary. 🙂