开发者

Why does ArrayList have "implements List"? [duplicate]

开发者 https://www.devze.com 2023-01-29 05:44 出处:网络
This question already has answers here: Why do many Collection classes in Java extend the abstract class and implement the interface as well?
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. 开发者_如何转开发

In the Collection Framework we have the interface List and the class AbstractList:

AbstractList implements List

And ArrayList extends AbstractList and

implements List

My question: why does ArrayList have the implements List clause?

If ArrayList extends AbstractList and AbstractList implements List, can't we say, that ArrayList implement List?


Yes. It could've been omitted. But thus it is immediately visible that it is a List. Otherwise an extra click through the code / documentation would be required. I think that's the reason - clarity.

And to add what Joeri Hendrickx commented - it is for the purpose of showing that ArrayList implements List. AbstractList in the whole picture is just for convenience and to reduce code duplication between List implementations.


My 2 cents is to keep to the fact that ArrayList is a List. AbstractList just completes certain implementations that the List requires.


Most likely this is to increase tracability of the inheritance structure. That way you don't have to go down the whole inheritance tree, when browsing the Javadoc or the like.

0

精彩评论

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