Why extends HashMap and implements Map?

Hi,

I browsed the source of HashMap in JDK 5.0.

The declaration of the class puzzled me.

Declaration of HashMap:publicclass HashMap<K,V>

extends AbstractMap<K,V>

implements Map<K,V>, Cloneable, Serializable

Declaration of AbstractMap:publicabstractclass AbstractMap<K,V>implements Map<K,V>

Puzzle:

Now that AbstractMap has been declared to implement Map interface, and HashMap extends AbstractMap,

why HashMap is declared to implement Map interface again?

I don't think that's necessary.

a cup of Java, cheers!

Sha Jiang

[899 byte] By [jiangshachinaa] at [2007-11-27 6:32:02]
# 1
Try removing the Map<K, V> from the implements clause and see what happens.As I recall, it's not necessary to specify that it implements Map here for the reasons you gave. Maybe the coder added that just for clarity.But either way you can tell with a test.
paulcwa at 2007-7-12 17:57:12 > top of Java-index,Java Essentials,Java Programming...
# 2
> Try removing the Map<K, V> from the implements clause and see what happens.How can I edit sources of JDK API?I know JDK provides a src.zip, I could edit the source in that archive file.But I have to compile the source I edit, that may be a little
jiangshachinaa at 2007-7-12 17:57:12 > top of Java-index,Java Essentials,Java Programming...
# 3
Oops my mistake. I missed that part of your post.But you can easily write your own test in a standalone class.
paulcwa at 2007-7-12 17:57:12 > top of Java-index,Java Essentials,Java Programming...
# 4
If with my own test, I'm almost sure of it's not necessary to do the declaration.
jiangshachinaa at 2007-7-12 17:57:12 > top of Java-index,Java Essentials,Java Programming...
# 5
I agree with you; I don't think it's necessary to declare that it implements Map if it extends AbstractMap; I don't think it makes any difference that this code is part of the standard library; and I think it was declared as such for clarity only.
paulcwa at 2007-7-12 17:57:12 > top of Java-index,Java Essentials,Java Programming...
# 6
> I don't think it makes any difference that this code is part of the standard libraryAbsolutely, I agree on the view.> I think it was declared as such for clarity only.I just be curious with the little topic."clarity" may be the answer.Thanks!
jiangshachinaa at 2007-7-12 17:57:12 > top of Java-index,Java Essentials,Java Programming...
# 7
Wasn't there a thread about this just a while ago?About how it actually does affect the class, when using reflection to find out which interfaces it implements. At least as far as I remember. Someone confirm or deny.
-Kayaman-a at 2007-7-12 17:57:12 > top of Java-index,Java Essentials,Java Programming...