Class to Interface

Hi You all,

I have been coding for a while, but never really cared about this...

If Map is an interface, it just has method signatures... If I do the following:

Map map =new HashMap();

How does map have any concrete implementations since it is just an interface, and why would not one do:

HashMap map =new HashMap();

I have been doing this for a while, but some days we just make this questions ourselves, and this is one of those days.

Thanks for any info ;)

MeTitus

[590 byte] By [Me_Titusa] at [2007-11-26 12:40:25]
# 1

> Hi You all,

>

> I have been coding for a while, but never really

> cared about this...

>

> If Map is an interface, it just has method

> signatures... If I do the following:

>

> >

> Map map = new HashMap();

>

>

This is a good practice.

> How does map have any concrete implementations since

> it is just an interface, and why would not one do:

Map provides the interface: the methods that must be implemented. HashMap provides the actual implementation. Assign any implementation of Map to the variable "map" and we can invoke any methods of Map on "map" because we know that the implementation is required to have them.

> >

> HashMap map = new HashMap();

>

>

>

> I have been doing this for a while, but some days we

> just make this questions ourselves, and this is one

> of those days.

Now consider what would happen if you wanted your variable "map" to use an implementation other than HashMap. You would have to change the declaration. In your example this is rather trivial, the distinction becomes more important when it's a parameter or exposed. Consider the following method:

public HashMap getKeyMap()

Let's say that's part of a class that has a Map which maps keystrokes to an action. What happens when I make my own implementation of Map, call it BidiMap, and I want to use that instead? You're in real trouble. Every piece of code that called getKeyMap() is going to have to be altered. If you had simply declared that getKeyMap() returns a Map then it can return any implementation of HashMap, not just Map.

public Map getKeyMap()

Now I can use a BidiMap, or a HashMap, or any other implementation of Map I come up with and do so without the code that uses my method having to know anything about it.

kablaira at 2007-7-7 16:11:55 > top of Java-index,Java Essentials,Java Programming...
# 2
Nice one kablair,Thank you very much, now I can have a good night sleep ;)MeTitus
Me_Titusa at 2007-7-7 16:11:55 > top of Java-index,Java Essentials,Java Programming...
# 3

> How does map have any concrete implementations since

> it is just an interface,

How can I talk to you assuming you're a human, even though there is no "human" implementation, just concrete man and woman instances?

How can an ArrayList store a String, if it only takes Objects as argument?

There's a difference between the type of a reference and the type of the instance the reference is pointing to. "Map m" creates a reference only. It does not create an instance of Map or anything else.

CeciNEstPasUnProgrammeura at 2007-7-7 16:11:55 > top of Java-index,Java Essentials,Java Programming...