• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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
 
I don't like that guy. The tiny ad agrees with me.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic