Why we use,
Map abc = new HashMap(20);
instead of
HashMap abc = new HashMap(20);
please help me to find out the differnce between these two.
would be grateful for help.
Why we use,
Map abc = new HashMap(20);
instead of
HashMap abc = new HashMap(20);
please help me to find out the differnce between these two.
would be grateful for help.
In the first case, the abc variable is of type HashMap, which is an implementation of the Map interface. If you change your mind later and want to use a TreeMap for instance, you will have to change many references to HashMap in your code, and is is possible you used peculiarities of HashMap where the generic Map operations are enough.
If you use the Map interface as type for you variable, you can change the implementation easily.
It is important when you design classes for reuse. If you have a method that takes a Map as argument, any Map implementation will be usable to call your method. Thus, the caller will be free to use the most suitable implementation.