wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Related to Collection class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Related to Collection class" Watch "Related to Collection class" New topic
Author

Related to Collection class

padmaratna kamble
Greenhorn

Joined: Apr 26, 2006
Posts: 26
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jass Singh
Ranch Hand

Joined: Mar 30, 2006
Posts: 52
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
 
GeeCON Prague 2014
 
subject: Related to Collection class