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

后端 未结 8 2066
青春惊慌失措
青春惊慌失措 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条回答
  •  独厮守ぢ
    2020-12-05 03:07

    The answer to your first question is that implementing List is a contract. That contract can be defined by both AbstractList and ArrayList. ArrayList implements List to publish the fact that will respect the List contract in the future when it might be necessary to extend not from AbstractList that may or may not implement a List.

    For the second question: Arrays.asList returns a List. It could happen that in the current implementation returns ArrayList. In the next version could return a different list LinkedList for example and the contract(defined by the method signature) will still be respected.

提交回复
热议问题