File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quality Program ing

 
John Spindler
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks,
brian
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interestingly this exactly question (almost) appeared on beginner board recently. Click here to see what responses were given.
 
John Spindler
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh that's great! question answered, thanks!
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic