• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Lists

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List lst =new ArrayList()
ArrayList lst=new ArrayList()

What is difference b/w the above two statements.
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!