File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Quality Program ing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Quality Program ing" Watch "Quality Program ing" New topic

Quality Program ing

John Spindler

Joined: Feb 12, 2002
Posts: 28
Could someone please explain to me the benefits/disadvantages of writing something like:
Map m = new TreeMap();
versus writing:
TreeMap m = new TreeMap();
I used maps just because that is what I am working on, but it goes in general. Is there an advantage of creating an object by naming it's superclass during instantiation?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Interestingly this exactly question (almost) appeared on beginner board recently. Click here to see what responses were given.

Java API Documentation
The Java Tutorial
John Spindler

Joined: Feb 12, 2002
Posts: 28
oh that's great! question answered, thanks!
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
In fact this is a special case of the general OOD principle that you want your classes to be as loosely coupled as possible. You want to make as few assumptions as you can get away with.
For example, if your method takes an ArrayList argument, it probably should take a List instead. If all it does is iterate through the List and it never really use its List-ness, then it probably shouldn't even take a List but a Collection. And this doesn't just apply to the classes and interfaces from the Java Collections framework. It applies to your own classes and interfaces as well.
- Peter
[ May 22, 2003: Message edited by: Peter den Haan ]
I agree. Here's the link:
subject: Quality Program ing
It's not a secret anymore!