• 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

interface

 
Greenhorn
Posts: 5
MyEclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between the following two?

List l=new ArrayList();

and

ArrayList al=new ArrayList();

??
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

michael ngangom wrote:What is the difference between the following two?

List l=new ArrayList();

and

ArrayList al=new ArrayList();

??



in case of List l = new ArrayList(); the reference variable l is of type List which is an interface whereas in ArrayList al = new ArrayList(); al is of type ArrayList which is a class. well that was obvious you could say. it is said that it is a good practice to code in terms of interfaces. why ? suppose you have a method which takes list argument as follows :
method2(List list){} now this method can take any type of List. it can take ArrayList. suppose later you changed your implementation and supplied LinkedList , it will work. this flexibility does not come with the second option .
 
michael ngangom
Greenhorn
Posts: 5
MyEclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! That clears my doubt!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic