• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

List implementation

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always...always...its not true, it depends on what you want to do.
arno
 
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic