Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LinkedHashMap extends HashMap implements Map

 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May i know why LinkedHashMap extends HashMap implements Map while HashMap is already implements Map ?

When LinkedHashMap implements Map methods are available in HashMap and those behavior can be overridden.

does it make any sense?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let me ask it this way: how does it not make sense?
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:let me ask it this way: how does it not make sense?


there are 2 versions of same methods available (that is in Map) in the LinkedHashMap 1 from HashMap + 1 from Map

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rauhl Roy wrote:there are 2 versions of same methods available (that is in Map) in the LinkedHashMap 1 from HashMap + 1 from Map

Really? Is that what an interface means?

Think about it.
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Rauhl Roy wrote:there are 2 versions of same methods available (that is in Map) in the LinkedHashMap 1 from HashMap + 1 from Map

Really? Is that what an interface means?

Think about it.


Interface lets the implementing class define the behavior.

Here the confusion is why both Map's abstract methods and HashMap's implemented (Map's) methods are available in LinkedHashMap?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The interface merely declares what methods are available -- it says nothing about the implementation. So having the interface on two classes in the hierarchy introduces no ambiguity.

It's like if a person says: "I'm a police officer. I'm a police officer." Just because he said the same thing twice doesn't mean there's more than one police officer.
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:The interface merely declares what methods are available -- it says nothing about the implementation. So having the interface on two classes in the hierarchy introduces no ambiguity.

It's like if a person says: "I'm a police officer. I'm a police officer." Just because he said the same thing twice doesn't mean there's more than one police officer.


You mean reputation occurs but it is ignorable?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My point is there is nothing to be ignored. The same interface is being referenced. But it's the same interface, so there's not ambiguity.

 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:My point is there is nothing to be ignored. The same interface is being referenced. But it's the same interface, so there's not ambiguity.



So, LinkedHashMap will have a implemented version from HashMap.

when interface Map is implemented by and HashMap and LinkedHashMap is extending HashMap anyway.. what is the use of LinkedHashMap implements Map?

 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rauhl Roy wrote:what is the use of LinkedHashMap implements Map?


One use of that is this:
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Rauhl Roy wrote:what is the use of LinkedHashMap implements Map?


One use of that is this:



very good answer but can you please give any thing in terms of functionality usage?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rauhl Roy wrote:very good answer but can you please give any thing in terms of functionality usage?


I don't think so. For one thing I don't understand what "functionality usage" means.
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Rauhl Roy wrote:very good answer but can you please give any thing in terms of functionality usage?


I don't think so. For one thing I don't understand what "functionality usage" means.



i was talking about using the method in different way. {forget about it} but any other use other than the one you mentioned above?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rauhl Roy wrote:but any other use other than the one you mentioned above?


That is what it means for a class to implement an interface.
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Rauhl Roy wrote:but any other use other than the one you mentioned above?


That is what it means for a class to implement an interface.


so LinkedHashMap is implementing Map interface for object reference purpose?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rauhl Roy wrote:so LinkedHashMap is implementing Map interface for object reference purpose?


Of course. That is what interfaces are for.
 
Ankur Kapoor
Greenhorn
Posts: 11
Java Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Rauhl Roy wrote:what is the use of LinkedHashMap implements Map?


One use of that is this:


For this LinkedHashMap need not be implementing Map interface, merely extending HashMap would solve this as HashMap is already implementing Map.

Good point raised by Rahul, either this is not required or there must be some other reason(s) associated with it, need to dig more into it...
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:The interface merely declares what methods are available -- it says nothing about the implementation. So having the interface on two classes in the hierarchy introduces no ambiguity.

It's like if a person says: "I'm a police officer. I'm a police officer." Just because he said the same thing twice doesn't mean there's more than one police officer.


and also It is much more easy to find that a class implements which interfaces. Otherwise we will have to look the entire class hierarchy to find out all implemented interfaces by that class.

For example:

LinkedList extends AbstractSequentialList
AbstractSequentialList extends AbstractList
AbstractList implements List interface.

we dont have to go 2 levels up the hierarchy to find that LinkedList implements List interface.

so LinkedList class declaration makes it easy for us by writing:

class LinkedList<E> extends AbstractSequentialList<E> implements List<E>

now we can directly see that LinkedList implements List.
 
Ankur Kapoor
Greenhorn
Posts: 11
Java Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I suspect is that this is a case of applying 'Dependency Inversion Principle' while implementing LinkedHashMap, its hypothetical but assuming in future if HashMap might not implement Map then the developers can know at hands the associated sub-class(es) which are implementing Map interface and to incorporate those methods prior to which they were dependent upon HashMap's method.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50248
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It also makes it easier to find LinkedHashMap in the API documentation. If you missed out implements Map<K, V>, then you could not find LinkedHashMap from the documentation for Map. You would have to go to HashMap and look for subclasses.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic