File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interface Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "interface" Watch "interface" New topic
Author

interface

michael ngangom
Greenhorn

Joined: Aug 24, 2010
Posts: 5

What is the difference between the following two?

List l=new ArrayList();

and

ArrayList al=new ArrayList();

??


Michael.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 867

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 .


OCPJP 6(100 %) OCEWCD 6(91 %)
michael ngangom
Greenhorn

Joined: Aug 24, 2010
Posts: 5

Thanks! That clears my doubt!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface
 
Similar Threads
readonly ArrayList
why some classes are called deprecated & some legacy?
basic question about ArrayList
How do you avoid an ArrayList object from being modified, i.e. avoid adding and deleting its content
Collections.copy method for lists ? how it works