When I was seeing the declaration of ArrayList
class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
which implements List
interface even though ArrayList
's superclass AbstractList
implements the same List
interface.
abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>
Similar declarations can be found on HashMap
, LinkedHashMap
declarations also.
In the declaration of LinkedHashMap
, it implements Map
interface only and not the other interfaces implemented by its superclass HashMap
.
So there might be some benefits of having such declarations.
If you only want to inherit method signatures (name, arguments, return type) in the subclasses, use an interface, but if you also want to inherit implementation code, use a superclass.
Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class.
Implementing an interface enforces your class to be bound to the contract (by providing the appropriate members). Consequently, everything that relies on that contract (a method that relies on the functionality specified by the interface to be provided by your object) can work with your object too.
Interface is nothing but its a guide line for the new implementation it provides some instructions for new implementation and categorize the functionality of the object . In details like if we create an interface then we create an instruction for the implementation .
There are no functional benefits to declaring them again, it does not affect the behavior in any way.
I guess it's only added to make it clearer which interfaces are implemented.
This is done for documentation purposes only, to make it immediately clear to the user of the class which interfaces the class implements.
The redundant implements
clause makes no difference to the compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With