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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With