File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using List interface

 
Sumukh Deshpande
Ranch Hand
Posts: 87
Firefox Browser IntelliJ IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

In many places I have seen developers doing something like this with ArrayList:


If at all I am sure that I need ArrayList only then what is the need of having declared it as a reference of List type?

I understand that there is a coding practice followed as Coding To an Interface, but in above case is that really needed?

Please correct me if I am wrong.

Thanks i n advance.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's almost always a good idea to code to the interface, especially if the instance ends up being passed to other methods or returned as a method value. Except for when the instance is created, code should only care that it is a List -- the fact that it is an ArrayList is not needed to be known anywhere else.
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List list = new ArrayList();

Here you are creating new object of ArrayList ok, after your development is done in testing phase if you found that ArraList is performing badly you want to have LinkedList. since List interface is implemeted by both you can easily replace the above code with
List list=new LinkedList();
without breadking any functionality
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic