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

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

    I believe, there is a reason. This is just my thought and I didn't find it anywhere in JLS.

    If I am a developer who is writing an API which is to be widely used, why will I do this?

    There is absolutely no reason of doing this, but consider this scenario, where I have written the List interface and provided the ArrayList implementation for the the List interface.

    I have not yet written any abstract class AbstractList till now.

    One day a requirement comes, where I am asked to write few more implementations of the List interface where most of them are having similar or same concrete methods for the abstract methods in List interface.

    I will go ahead and write an AbstractList with necessary implementation for all those methods. But now I will not like that half of my classes to implement the List interface and half of them extending AbstractList.

    Also, I cannot just go and remove the 'implements List` from the classes I wrote earlier, might be because this is not the right time or I do not want other's code to break with my new release.

    Note This is solely my opinion.

提交回复
热议问题