Java: why does WeakHashMap implement Map whereas it is already implemented by AbstractMap? [duplicate]

StackOverflow https://stackoverflow.com/questions/14062286

  •  12-12-2021
  •  | 
  •  

Question

Possible Duplicate:
Java.util.HashMap — why HashMap extends AbstractMap and implement Map?
Why would both a parent and child class implement the same interface?

WeakHashMap<K,V> is declared to both extend AbstractMap<K,V> and implement Map<K,V>.

But AbstractMap<K,V> already implements Map<K,V>. It looks like the implements declaration is redundant.

What is the reason it was declared?

Was it helpful?

Solution

Barring someone being able to point to a reason posted online somewhere by the authors of WeakHashMap in the JDK, we can only speculate. The speculation is that it improves the auto-generated documentation. It has no effect on the interfaces exposed by the class or how you use it.

OTHER TIPS

Although it is indeed redundant, it ensures that if the interface implementation is removed from the parent, the child will still hold by the Map interface and produce the necessary compile errors.

This "looks" like an overlook. If you extend a class, you automatically implement all interfaces implemented by the base class (whether that base class is abstract or not). So, given:

public interface I {}

public class A implements I {}

and you declare:

public class B extends A {}

the declaration above is strictly equivalent to

public class B extends A implements I {}

An overlook indeed. Good spotting ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top