This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Lists" Watch "Lists" New topic
Author

Lists

Raj Joe
Ranch Hand

Joined: Oct 15, 2004
Posts: 41
List lst =new ArrayList()
ArrayList lst=new ArrayList()

What is difference b/w the above two statements.
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Hi,

The key is polymorphism. In the first line of code, we can only have access to the methods defined in the List interface. Additional methods defined in ArrayList which implements List, cannot be used by the first line of code.

For e.g.,



HTH.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Originally posted by Cheng Wei Lee:
Hi,

The key is polymorphism. In the first line of code, we can only have access to the methods defined in the List interface. Additional methods defined in ArrayList which implements List, cannot be used by the first line of code.



This is not exactly true, in the first line of code we can only access the methods defined in the List interface, unless we explicitly downcast the instance to an ArrayList.
For e.g.


It should be noted however that downcasting is not recommended unless, one is sure that the superclass object is a subclass object.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Nigel Browne:
It should be noted however that downcasting is not recommended unless, one is sure that the superclass object is a subclass object.
And you can do that with
Raj Joe
Ranch Hand

Joined: Oct 15, 2004
Posts: 41
Originally posted by Cheng Wei Lee:
Hi,

The key is polymorphism. In the first line of code, we can only have access to the methods defined in the List interface. Additional methods defined in ArrayList which implements List, cannot be used by the first line of code.

For e.g.,



HTH.



I have seen that people recommend the below style
List listA = new ArrayList();
Is there any advantage?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Raj Joe:

I have seen that people recommend the below style

List listA = new ArrayList();

Is there any advantage?


Yes. It you write code this way, then if you later decide that a LinkedList would work better in your application, then you can just change this one line of code, and everything is guaranteed to still work. In general, using the least specific possible reference types makes your application easier to modify.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lists
 
Similar Threads
Reverse the order of elements in a ArrayList
updating list dynamically on the frame
Difference between Interface reference and class reference
List vs ArrayList
Diffrence between declaring List lst = new ArrayList() and ..