Is there any benefit in implementing a interface in a subclass even though the superclass implements the same interface

前端 未结 6 1542
醉酒成梦
醉酒成梦 2021-01-12 05:29

When I was seeing the declaration of ArrayList

class ArrayList extends AbstractList
    implements List, RandomAccess         


        
6条回答
  •  感动是毒
    2021-01-12 05:38

    ArrayList and AbstractList implement List for different purposes.

    • List establishes what is required for a class to be a list.
    • ArrayList implements List as part of defining its own interface. This is essential to what ArrayList is.
    • ArrayList extends AbstractList as part of its own implementation. This is entirely optional: one could have implemented ArrayList from scratch without inheriting AbstractList, and the class would work in the same way.
    • AbstractList is intended as a base class for other implementations of List. Instead of establishing an interface, it follows an existing one. AbstractList's implementation of List is not required, everything would compile and run without it just the same. However, inheriting List lets Java compiler spot potential discrepancies between the interface methods and the methods of AbstractList, so it is a very good idea for AbstractList to implement List interface.

提交回复
热议问题