Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Related to Collection class

 
padmaratna kamble
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jass Singh
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • 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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic