Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map and TreeMap Instantiation

 
kalpana Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Basic Question:
I want to know the advantages in instantiating the TreeMap as
A. Map testmap = new TreeMap();
B. TreeMap testmap = new TreeMap();
I am just trying to use TreeMap but when I see the examples, I see most of them have used A for getting an instance.
I would like to know the exact details as to why it is done so and how different is it from 'B'.

Thanks
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it's complicated ... kinda. Basically, the idea is to limit the knowledge of the actual type of object, but instead restrict it to some lesser type. In this case, the rest of your program doesn't really need to know it's actually a TreeMap, so it's typed as a Map instead.

The official OO word is "coupling". By reducing the coupling between your program and the type of the object, it makes other things in software development easier. For instance, what would happen if you suddenly find that a TreeMap doesn't suit your purposes?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this question has been asked here before, but I'm having trouble finding the posts with the Search tool. I was hoping to give a link for you, but I guess I'll just let you experiment with keywords for a search. If I find anything, I'll let you know.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic