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

后端 未结 8 2070
青春惊慌失措
青春惊慌失措 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:10

    For your first question take a look at Why does ArrayList have "implements List"?


    To answer your second question

    java.util.ArrayList a = Arrays.asList(stra);
    

    as you mentioned Arrays.asList returns its own implementation of AbstractList and unfortunately creators of this code also named this class ArrayList. Now because we cant cast horizontally but only vertically returned array list can't be cast to java.utli.ArrayList but only to java.util.AbstractList or its super types like java.util.List that is why your first code example works.

提交回复
热议问题