• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is the difference

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



List l=new ArrayList();
ArrayList al=new ArrayList();



What is the difference between the above 2 code snippets.?
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both ArrayList Objects are same. The first one is accessed using an interface reference while the second one is accessed using ArrayList reference. The difference is that in first declaration of ArrayList you can only call those methods which are defined in List Interface.

ArrayList class implements List Interface so it implements all methods given in List Interface and also has some methods which are specific to ArrayList. Those methods which are specific to ArrayList cannot be called using reference of its Interface(List).


HTH
 
Marshal
Posts: 79174
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It also means you can change the implementation and use a LinkedList instead, by saying

List<Foo> fooList = new LinkedList<Foo>();

or keep the ArrayList

List<Foo> fooList = new ArrayList<Foo>();
 
Krishna prasad ambala
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Friends for giving some usefull info abt my code.

Advanced Happy New Year.

[edit]Get rid of UPPERCASE. CR[/edit]
[ December 31, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 79174
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Krishna prasad ambala, please don't write all in uppercase; I have edited your post.
Read this FAQ, please.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic