| Author |
Using List interface
|
Sumukh Deshpande
Ranch Hand
Joined: Feb 17, 2008
Posts: 87
|
|
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
Joined: Jan 10, 2002
Posts: 56229
|
|
|
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.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Malatesh Karabisti
Ranch Hand
Joined: Jul 28, 2010
Posts: 143
|
|
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
|
 |
 |
|
|
subject: Using List interface
|
|
|