2

Normally, when you create an object you provided the same type in front like:

Scanner scanner = new Scanner(System.in);

but declaring a HashMap object and TreeMap follow a different syntax like this.

Map m1 = new HashMap();
SortedMap sm = new TreeMap();

What is the reason? I asked my professor. But he did not know the answer.

Nicholas
  • 679
  • 2
  • 11
  • 29
  • http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface – jmj Aug 13 '12 at 05:47
  • Thanks. Reading this right now. – Nicholas Aug 13 '12 at 05:51
  • By the way, you can also do `Map sm = new TreeMap()". That would convey that you want the keys sorted, but you don't actually need any of the methods that take advantage of that. One use case is if the code only needs to put/get entries, but you then intend to print them out in a human-readable way, where it's convenient to have the entries print out in order. – yshavit Aug 13 '12 at 06:13

4 Answers4

5

but declaring a HashMap object and TreeMap follow a different syntax like this.

You don't have to. You can write:

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

... it's just that you usually don't.

Likewise you could write:

Object scanner = new Scanner(System.in);

Basically, there are two types involved:

  • The type of the variable you're declaring
  • The type whose constructor you're calling

They don't have to be the same, but the constructed type does have to be assignment compatible with the variable type. It has to be a superclass or an interface supported by the class. The point of only specifying a Map (or List or whatever) variable as just the interface type is that most of the code should only think about it as a map/list/set/whatever. The fact that it happens to be a HashMap (or ArrayList etc) under the covers is an implementation detail.

See "programming to an interface" for more details of this... but stay aware that the syntax is the same in both cases:

VariableType variableName = new ActualTypeBeingConstructed();

I asked my professor. But he did not know the answer.

That scares me...

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Map is an interface (or more general class) - TreeMap is something that implements Map (or is a more specific class).

e.g. List is a general interface, ArrayList implements List so you can say

List<Obj> list = new ArrayList<Obj>();
// or
ArrayList<Obj> list = new ArrayList<Obj>();
// but not
ArrayList<Obj> list = new List...

If your professor really can't answer this, you really need to get a new Professor!!

John3136
  • 28,809
  • 4
  • 51
  • 69
  • 1
    Assuming the subject is programming or computer science or something. If the prof. can answer but wanted you to find out for yourself: Ok, I'll buy it. If the prof. seriously couldn't answer, then: No. He might be a nice guy, but he doesn't know the subject matter and is not fit to be teaching it! – John3136 Aug 13 '12 at 05:57
1

SortedMap is an interface so you can not instanciate it . Thats why we create an child class object TreeMap which implements this interface and assign it to interface object SortedMap so that it can use child and parent both class's functions.

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
1

You can also declare

HashMap m1 = new HashMap();
TreeMap sm = new TreeMap();

Map is an interface that is implemented by HashMap (and TreeMap). You can assign any object that implements the Map interface to a variable decalared as a map. Similarly for the Interface TreeMap.

In java you have a Hierarchy of class's and interfaces. A class is assignable to any "parent" class of any interface it implements

A more complicated example is the Swingclass JTextField The hierachy here is

Component
  Containter
     JContainer
         JTextComponent
             JTextField

So

 Component c1 = new JTextField();
 Containter c2 = new JTextField();
 JContainer c3 = new JTextField();
 JTextComponent c4 = new JTextField();

are all perfectly valid. A JTextField is a JTextCompent, JContainer, Container and Component.

Bruce Martin
  • 10,358
  • 1
  • 27
  • 38