Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does HashMap implement Map if it extends AbstractMap? [duplicate]

Possible Duplicate:
Java.util.HashMap — why HashMap extends AbstractMap and implement Map?

In java to implement HashMap<K,V> we need to implement Map<K,V>.

However when I debugged more in java classes it seems that.... java defines HashMap class as following.

public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable

At the same time i saw public abstract class AbstractMap<K,V> implements Map<K,V> it also implements the interface Map<K,V>.

If abstract class implements the interface then, what is the reason behind implementing Map<K,V> at HashMap class level?

As per my understanding HashMap class have all the methods inherited from AbstractMap which can be overridden by HashMap as per the requirement.

like image 550
Prasad S Deshpande Avatar asked Jun 24 '12 04:06

Prasad S Deshpande


1 Answers

It's probably just there to make things more obvious. You can basically directly see from the code of that single class that HashMap implements the Map interface. Yes, it already extends AbstractMap, but that's probably only considered an implementation detail.

There's nothing wrong with implementing interfaces again. That doesn't change how the code is compiled, but it definitely helps because you see it immediately. You don't have to climb up the class hierarchy or load the API docs first.

like image 161
Wormbo Avatar answered Oct 10 '22 01:10

Wormbo