aspose file tools*
The moose likes Java in General and the fly likes LinkedHashMap extends HashMap implements Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "LinkedHashMap extends HashMap implements Map " Watch "LinkedHashMap extends HashMap implements Map " New topic
Author

LinkedHashMap extends HashMap implements Map

Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

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

Joined: Jan 10, 2002
Posts: 61224
    
  66

let me ask it this way: how does it not make sense?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

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

Joined: Jan 10, 2002
Posts: 61224
    
  66

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

Joined: Aug 01, 2006
Posts: 401

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

Joined: Jan 10, 2002
Posts: 61224
    
  66

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

Joined: Aug 01, 2006
Posts: 401

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

Joined: Jan 10, 2002
Posts: 61224
    
  66

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

Joined: Aug 01, 2006
Posts: 401

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Rauhl Roy wrote:what is the use of LinkedHashMap implements Map?


One use of that is this:
Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Aug 01, 2006
Posts: 401

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Aug 01, 2006
Posts: 401

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Rauhl Roy wrote:so LinkedHashMap is implementing Map interface for object reference purpose?


Of course. That is what interfaces are for.
Ankur Kapoor
Greenhorn

Joined: Sep 13, 2006
Posts: 11

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...


Ankur Kapoor

Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

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.


Piyush
Ankur Kapoor
Greenhorn

Joined: Sep 13, 2006
Posts: 11

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

Joined: Oct 13, 2005
Posts: 38873
    
  23
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.
 
Don't get me started about those stupid light bulbs.
 
subject: LinkedHashMap extends HashMap implements Map