Charles Green Way
Charles Green Way

Reputation: 95

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

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.

enter image description here

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.

Upvotes: 8

Views: 256

Answers (6)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

ArrayList<T> and AbstractList<T> implement List<T> for different purposes.

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

Upvotes: 0

ZhongYu
ZhongYu

Reputation: 19682

Totally unnecessary. I wouldn't do it at all.

It's unclear why they did that by then. But by now apparently it's a mistake, since everybody is surprised by it when they first notice this odd redundancy.

Upvotes: 1

Waqas Memon
Waqas Memon

Reputation: 1249

Yes. It could've been omitted. But thus it is immediately visible that it is a List. Otherwise an extra click through the code / documentation would be required. I think that's the reason - clarity.

And to add what Joeri Hendrickx commented - it is for the purpose of showing that ArrayList implements List. AbstractList in the whole picture is just for convenience and to reduce code duplication between List implementations.

Reference: Why does ArrayList have "implements List"?

Upvotes: 1

PurkkaKoodari
PurkkaKoodari

Reputation: 6809

Well, this way you must implement List<E> methods when you create a subclass to AbstractList, and you can also use an ArrayList as an AbstractList.

Upvotes: 0

NPE
NPE

Reputation: 500257

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.

Upvotes: 3

Keppil
Keppil

Reputation: 46209

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.

Upvotes: 6

Related Questions