• 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

Related to Collection class

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we use following ways to create a collection object.
1.List l1=new ArrayList();
2.ArrayList al=new ArrayList();
3.Collection c=new ArrayList();
Why we use the first one more often but not the remaining two?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

This is a Java question, but you've posted it in our HTML and JavaScript forum -- so it's off-topic here. I'm going to move this over to our Java in General(Beginner) forum, where someone will answer it for you.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sachin kamble:
we use following ways to create a collection object.
1.List l1=new ArrayList();
2.ArrayList al=new ArrayList();
3.Collection c=new ArrayList();
Why we use the first one more often but not the remaining two?



Well all 3 are valid ways.

List is an Interface which has follwoing implementing classes:
-ArrayList
-LinkedList
-Vector

Now you are using List l1=new ArrayList();. But suppose after sometime due to some design consideration or whatever you decide to use Vector instead of ArrayList then by simply changing 1st line to List l1=new Vector(); you can do that, without touching any other part of your code.

In fact it best practice to use parent Interfacer refference while creating object of implementing class.

Regards,
Jass
 
reply
    Bookmark Topic Watch Topic
  • New Topic