1

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?

Community
  • 1
  • 1
Oz Molaim
  • 2,016
  • 4
  • 20
  • 29

3 Answers3

3

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • The generated javadoc for a class depends on the doclet class used, and it happens that the default doclet queries for interfaces "deeply", so it does not matter whether you omit redundant interfaces or not: they will apear in the javadoc. – fge Dec 27 '12 at 23:06
  • @fge: I ain't disagreein'. :-) I want to believe the Sun engineers (as was) did this for a reason. I don't, but I want to... – T.J. Crowder Dec 27 '12 at 23:09
2

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.

cklab
  • 3,761
  • 20
  • 29
  • If you rebased it, and the new base didn't implement `Map`, **and** you didn't add the `implements` and necessary plumbing, that would be a pretty shocking edit. :-) – T.J. Crowder Dec 27 '12 at 22:55
1

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 ;)

fge
  • 119,121
  • 33
  • 254
  • 329