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