This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List implementation

 
padmaratna kamble
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends ........
please tell me we always use
List l=new ArrayList();
and Collection c=new ArrayList();

why we not always use

ArrayList a=new ArrayList();
 
Arno Reper
Ranch Hand
Posts: 286
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always...always...its not true, it depends on what you want to do.
arno
 
Pavel Kubal
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by padmaratna kamble:
hello friends ........
please tell me we always use
List l=new ArrayList();
and Collection c=new ArrayList();

why we not always use

ArrayList a=new ArrayList();



Imagine situation. You have a class which uses 20 times your once created ArrayList instance. In a newer JVM appears better implementation for List interface and you would like to use it, but you have to rewrite whole class!
If only you had used interface, you could rewrite only one line.

For better design of your api is very, very useful to access classes via their interfaces.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a basic Object Orientation principle: program to an interface, not to an implementation.

That way you can easily interchange implementations changing just a few lines of code.
 
Naseem Khan
Ranch Hand
Posts: 809
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the following two cases...
Case: 1



In the above code, the instance of ArrayList is referred by ArrayLiist reference variable. Now suppose there is a slight change in ur requirement. Instead of ArrayList u want to use LinkedList, then u have to make changes at two different places in ur code. An ArrayList reference variable can't refer ny LinkedList instances.

U code will become..



Case 2:



change at one place only. Only change ArrayList constructor to LinkedList constructor like this.



This is the obvious advantage with second code.

One more thing in case 2, polymorphic feature of java is used which is not in case 1.

regards

Naseem Khan
[ May 20, 2006: Message edited by: Naseem Khan ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic