开发者

Java.util.HashMap -- why HashMap extends AbstractMap and implement Map? [duplicate]

This question already has answers here: 开发者_运维问答 Why do many Collection classes in Java extend the abstract class and implement the interface as well? (10 answers) Closed 6 years ago.

why HashMap extends AbstractMap and implement Map ? is extending AbstractMap not sufficient, because AbstractMap implements Map?


It is redundant. I suspect that it was done for "documentation" reasons. HashMap implements Map, and you can rely on that. The fact that it extends AbstractMap is arguably just an implementation detail. (Though it's extremely unlikely that future versions of HashMap would not extend AbstractMap, since there probably some code out there that expects HashMap to be assignable to AbstractMap.)


It is redundant, but doesn't hurt. If they do not want to implement Map in AbstractMap in future, it will still work.


One more thing is that AbstractMap is an implementation detail. It could be absent in a future version of Oracle's JDK.. It could be absent in a current version of IBM's JDK.. But whether it is present or not, the HashMap should always conform to the List contract.. So, it is explicitly captured in class declaration by specifying 'HashMap implements Map'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜