I was looking at Map and SortedMap documentation from Java 7 and I have realized that SortedMap
, which extends Map
overrides entrySet()
, keySet()
and values()
.
AFAIK, interfaces cannot implement a method in Java prior to Java 8. So what was the reason for overriding these methods?
Best Answer
It looks as though the methods were overridden solely so that different documentation could be provided.
For all three methods, the only difference in documentation is that the
SortedMap
version adds "The set's iterator returns the entries in ascending key order." to the main description and "sorted in ascending key order" to the return value description.P.S. The obvious follow-up question is why weren't these three method signatures simply changed to return
SortedSet
instead ofSet
. Unfortunately I can't come up with a reason for this, as the Java language seems to allow this sort of change, and the additional methods onSortedSet
seem trivial to implement.