• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

LinkedHashMap extends HashMap implements Map

 
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 28322
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28322
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28322
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28322
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
Postgres Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Postgres Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79949
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic