19

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.

Community
  • 1
  • 1
Prasad S Deshpande
  • 1,362
  • 4
  • 15
  • 35

4 Answers4

6

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.

Wormbo
  • 4,978
  • 2
  • 21
  • 41
6

In this particular case, it purely for documentation purposes; i.e. to make it clear to the reader that this is a Map implementation. I'm pretty sure there is negligible cost in this bit of redundancy.

(And, yes, your understanding is correct.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
3

The "implements Map" is optional and is usually there to help people reading the code that HashMap implements Map's interface methods as well as AbstractMap's abstract methods.

pek
  • 17,847
  • 28
  • 86
  • 99
3

I believe the reasoning behind this is an abstract class in Java need not declare/implement all the methods in the interface. Thus

public interface MyInterface{
  void a();
  void b();
  void c();
}

the following abstract implementation of the interface is valid.

public abstract class AbstractClass implements MyInterface {
  public void a() {}
  public void c() {}
  public void d() {}
}

Thus I believe in order to be explicit about HashMap implementing the methods not implemented by the abstract class it is shown to implement the interface Map whereas it is completely optional to do so because any implementation of the abstract class needs to implement all the methods either in the abstract class or the derived base class.. Thus in the above example a valid implementation to the abstract class is

public class MyClass extends Abstract{
      public void a() {}
      public void c() {}
      public void b() {}  //if you dont implement this, compile error
      public void d() {}
    }

which you can rewrite as follows too:

public class MyClass extends Abstract implements MyInterface {
      public void a() {}
      public void c() {}
      public void b() {}
      public void d() {}
    }
Baz1nga
  • 15,485
  • 3
  • 35
  • 61
  • 5
    No reason to do this, if an abstract class implements an interface it doesn't have to implement all the methods, the first "non-abstract" class extending the abstract class __must__ have to implement the "not implemented" methods. – Luiggi Mendoza Jun 24 '12 at 05:35