Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 376
Eleco Avatar asked Aug 09 '10 09:08

Eleco


2 Answers

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.

like image 159
aioobe Avatar answered Sep 28 '22 10:09

aioobe


It's ancient code. Up to some point around JDK 1.1.6 or so, Javadoc didn't show inherited interfaces, so it was customary or indeed necessary to reiterate them in derived classes to get the Javadoc to work right. They were introduced in JDK 1.2 but were available well before that as an add-on for 1.1.x.

like image 42
user207421 Avatar answered Sep 28 '22 11:09

user207421