Why does arraylist class implement List as well as extend AbstractList?

后端 未结 8 2064
青春惊慌失措
青春惊慌失措 2020-12-05 02:29

The implementation of java.util.ArrayList implements List as well as extends AbstractList. But in java docs you can see that AbstractL

8条回答
  •  -上瘾入骨i
    2020-12-05 03:18

    Then wouldn't it be redundant to implement List as well as extend AbstractList?

    Yes, it is 100% redundant. However, Java implementors added interfaces very consistently in all public implementation of the collections library:

    • LinkedList and ArrayList extend AbstractList which implements List, and then implement List themselves.
    • HashSet and TreeSet extend AbstractSet which implements Set, and then implement Set themselves.
    • HashMap and TreeMap extend AbstractMap which implements Map, and then implement Map themselves.

    My understanding is that they did so for documentation purposes: the authors wanted to show that ArrayList is primarily a List; the fact that ArrayList extends AbstractList is a less significant detail of its implementation. Same goes for the other public collection types.

    Note that Arrays.ArrayList class is not publicly visible, so its authors did not care to include List explicitly.

    As far as the failed conversion goes, this should come as no surprise, because the inner class Arrays.ArrayList and the public class ArrayList are unrelated to each other.

提交回复
热议问题