GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 478 of 502    Print  
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();



  
Total Answers and Comments: 6 Last Update: July 13, 2009     Asked by: rgmanoj 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: rariedel
 
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.

Above answer was rated as good by the following members:
sandeep549, deepkrisna
May 23, 2008 15:18:54   #1  
rariedel Member Since: May 2008   Contribution: 8    

RE: Array List Collections
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.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
May 26, 2008 06:14:34   #2  
zizou Member Since: May 2008   Contribution: 2    

RE: Array List Collections
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.

 
Is this answer useful? Yes | No
June 15, 2008 17:07:30   #3  
interviewprep9 Member Since: June 2008   Contribution: 10    

RE: Array List Collections
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
 
Is this answer useful? Yes | No
June 27, 2008 05:35:11   #4  
manjot.kr Member Since: June 2008   Contribution: 1    

RE: Array List Collections
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

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
July 05, 2008 06:37:58   #5  
R.Raj Member Since: April 2008   Contribution: 3    

RE: Array List Collections
Yes It is possible to invoke the methods of ArrayList using the reference variable list.
Infact it is used for that purpose only.

 
Is this answer useful? Yes | No
July 11, 2009 09:05:04   #6  
shailesh.pandit Member Since: July 2009   Contribution: 1    

RE: Array List Collections
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.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape