Array List Collections

What is Use of declaring an array list object like
List list = new ArrayList() rather than creating object like ArrayList arr = new ArrayList();

Questions by rgmanoj

Showing Answers 1 - 18 of 18 Answers

rariedel

  • May 23rd, 2008
 

In the simple example given, there is no particular advantage; however, in many situations it is often useful to interact with the derived class using only the interface contract from which is was derived.

Using constructs of this type are useful when it is not known in advance which of its many potential derived class implementations will be actually be instantiated. This allows generic programming constructs to be applied to a plethora of potential run-time variations.

zizou

  • May 26th, 2008
 

Hi
this technique is much more advantageuous as it'easy to cast the created object in runtime easily to another subclass of the list super classs.

  Was this answer useful?  Yes

Can someone please explian more eloborately on this one? Using List list = new ArrayList(), will I be invoking list's methods or the ArrayList methods with the reference variable list

  Was this answer useful?  Yes

manjot.kr

  • Jun 27th, 2008
 

List is an interface where as Arraylist is a class.
Arraylist implements all the methods in List...
So the arraylist's methods are invoked as the method body is present in Arralist

R.Raj

  • Jul 5th, 2008
 

Yes,It is possible to invoke the methods of ArrayList using the reference variable list.
Infact it is used for that purpose only. 

  Was this answer useful?  Yes

We may decide later to use the LinkedList instead of ArrayList, in this situation we just need to replace the new ArrayList() part with new LinkedList() if we keep the referance variable as List interface.

whereas if we keep ArrayList as the referance variable it will be cumbersome.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions