0

Possible Duplicate:
Why are variables declared with their interface name in Java?
How should lists be cast to their conrecte implementations?

Example:

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

Thanks.

Community
  • 1
  • 1
Nate
  • 919
  • 2
  • 9
  • 18
  • This seems to come up a lot; see http://stackoverflow.com/questions/1484445/why-are-variables-declared-with-their-interface-name-in-java?rq=1 and http://stackoverflow.com/questions/11911554/when-do-i-want-to-declare-interface-over-the-actual-class – Zong Aug 18 '12 at 16:42
  • So Map is an interface? Is that different from a base class? – Nate Aug 18 '12 at 16:49

4 Answers4

3

This is usually so that you can swap in another implementation later and still have the code operate the same. For example, if you then decided to use a TreeMap instead of a HashMap, you could just change the instantiation step, and the rest of your code would still work just fine.

Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • I see. Makes sense. Can the methods declared in the derived class still be used? – Nate Aug 18 '12 at 16:42
  • @Nate No, so if you need to use them, you'll need to declare it as the subtype. However, once you do this, you're tied to that implementation, so it's considered good practice to try and only use the common interface if you can. – Alexis King Aug 18 '12 at 16:43
  • 1
    Also see polymorphism - an important OO concept. – Dan Aug 18 '12 at 17:44
3

It a programming practice called programming to an interface.

Community
  • 1
  • 1
Dave
  • 4,546
  • 2
  • 38
  • 59
1

You could switch implementations (e.g. in your example switch to a TreeMap) without affecting the rest of the code, since it will use the base class i.e. Map.
It decouples your code and is considered a good programming practice to code against an interface instead of a concrete implementation

Cratylus
  • 52,998
  • 69
  • 209
  • 339
1

Design by Contract or "Programming to an Interface" are principles that ensure that your code only uses the properties/methods exposed by the interface, and more importantly, one primary interface at any given time. If you're using multiple interfaces at a time, that is generally a sign of tighter coupling.

In general, you should not instantiate objects directly in your code either. You should delegate that job to an external entity so that any changes of the implementation used, can be done in a single place.

There was a Dr Dobb's Journal article that the 'new' keyword should be forbidden (exact title was something like 'new is verboten') but I can't find a link to it right now.

Rajesh J Advani
  • 5,585
  • 2
  • 23
  • 35
  • In the code in the question, would you also call the base class (Map) an interface? – Nate Aug 18 '12 at 16:57
  • `Map` *is* an interface. See the javadoc http://docs.oracle.com/javase/6/docs/api/java/util/Map.html . But specifically, an interface is a contract, so the argument map apply to both abstract classes as well as interfaces in java, depending on how your application is designed. The important concept is a 'contract' which is not a language specific idea. – Rajesh J Advani Aug 18 '12 at 17:01
  • I stand corrected. Thanks you. – Nate Aug 18 '12 at 17:04