Search code examples
javacollectionslinkedhashmap

LinkedHashMap signature


Looking at the JDK source code for LinkedHashMap, I noticed that this class is declared as:

 public class LinkedHashMap<K,V>
       extends HashMap<K,V>
       implements Map<K,V>
   {...

why the redundant "implements Map<K,V>" (since HashMap already implements Map) ? I cannot imagine this is a typo...

Thanks.


Solution

  • I suppose it's a way of saying

    No matter what interfaces HashMap implements (now or in the future), this class should implement the Map interface.

    If someone responsible for the HashMap decides that it should no longer implement the Map interface, the compiler will warn the maintainer of LinkedHashMap that it no longer implements the Map interface as he intended.

    Of course it's silly in this particular case (HashMap will obviously always be a Map), but similar situations may benefit from (and have given rise to) such convention.