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'.
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?
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.