Answer by DNA for Why the coding should be "to the interface" especially for...
The former allows you to change to:Map<Integer, String> mymap = new TreeMap<Integer, String>(); for example, without breaking all the rest of your code.A much more minor advantage is that...
View ArticleAnswer by pap for Why the coding should be "to the interface" especially for...
In the specific instance you propose, it may not make a difference, but it's good practice to get yourself used to always use the interface in your declarations because there are legitimate instances...
View ArticleAnswer by Matthias for Why the coding should be "to the interface" especially...
If you are only using this inside your type (meaning: private) and instantiate it yourself, it doesn't make a real difference.It starts getting interesting if the public interface of your type exposes...
View ArticleAnswer by khachik for Why the coding should be "to the interface" especially...
Using Map mymap allows you to change the implementation later. For example, if at some point you need mymap to be ordered, you just change the initialization to LinkedHashMap.
View ArticleAnswer by soulcheck for Why the coding should be "to the interface"...
The second option limits you to always use HashMap, even if some day TreeMap could be more useful. In the first one you can change the particular implementation easier - you only have to change one...
View ArticleAnswer by user647772 for Why the coding should be "to the interface"...
Because mymap can be instantiated somewhere else with a different implementation of Map, you should not rely on it being an instance of HashMap in the code using it.
View ArticleWhy the coding should be "to the interface" especially for primitive datatypes?
I have a doubt on the design methodology that why we implement the code to the interface. This is very much observed in primitive data types. Like I am not getting the difference between these two...
View Article