wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is the Advantage of Implementing List? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the Advantage of Implementing List?" Watch "What is the Advantage of Implementing List?" New topic
Author

What is the Advantage of Implementing List?

karthik manick
Ranch Hand

Joined: Aug 25, 2009
Posts: 52
In many places i have seen statement like this. List x=new ArrayList(); In Sun tutorial it is given like "In this way, the program does not become dependent on any added methods in a given implementation, leaving the programmer free to change implementations anytime that it is warranted by performance concerns or behavioral details" .

But i dont understand the significance of the above statement. why cant we use ArrayList x = new ArrayList? What is advantage of implementing a list?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42035
    
  64
The advantage is not having to change the type "ArrayList" everywhere you're using it later when you decide that -instead of ArrayList- you'd rather use a LinkedList, for example. You could simple write "List x = new LinkedList()" instead of "List x = new ArrayList()" and be done with it.

This is particularly important for method signatures. If a signature contains ArrayList for a parameter instead of List, then you may have to change a lot of code that uses the method (and it may not be possible at all for complex projects, or for libraries that get used by several client projects).


Ping & DNS - my free Android networking tools app
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Most of the time it's enough to know that you have a simple List. So it's much better to declare the variable type as List if this satisfies your needs because later you can easily switch the implementation say from ArrayList to LinkedList and you on have to change it in one line of code and not all over the place. If you directly depend on an ArrayList on the other hand you may (accidentally) use methods which are only available in ArrayList but not in the List interface. This would make it much harder to change the implementation later (for whatever reasons).

Oops, Ulf was obviously faster...

Marco
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Example, if you define as below,


then you may use a method specific to the LinkedList[example getLast(),getFirst()..etc]. in future if requirement change[or you want to implement ] to the ArrayList. how will you replace LinkedList dependent methods[ getLast()].

instead if you use like below



you cant use any method which is specific to the implementation class.

this is a very important future of Interface
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Marco Ehrentreich wrote:Oops, Ulf was obviously faster...


Even you
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Even you

Don't worry! At least we all used a LinkedList to explain the problem
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

also remember, that often you are writing code against what someone else does. you may buy it from another vendor, you may work for a huge company where you get it from another division...

Imagine if someone tells you their code returns an ArrayList(). You write hundreds of lines of code, making all your references ArrayLists and calling the specific methods for it.

then, the other group sends you an email saying "yeah, the ArrayList doesn't do what we need...we're going to change it to a LinkedList. sorry!!!"

Now you have to go in and change all your code - all the reference types, all the method calls, etc.


I wrote my code to expect a List. Since the LinkedList IS a list, I don't care that they change it. I get to spend my vacation on a warm island, while your stuck re-factoring all your code.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Marco Ehrentreich wrote:Don't worry! At least we all used a LinkedList to explain the problem


Yeah
karthik manick
Ranch Hand

Joined: Aug 25, 2009
Posts: 52
thanks to everyone. Now i am clear with that..
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

karthik manick wrote:thanks to everyone. Now i am clear with that..

Good and welcome
 
GeeCON Prague 2014
 
subject: What is the Advantage of Implementing List?