Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why to return an Interface (List) an not the Implemantation (ArrayList)?

Reply
Thread Tools

Why to return an Interface (List) an not the Implemantation (ArrayList)?

 
 
etienno
Guest
Posts: n/a
 
      02-26-2007
Hi,

I have a simple question about return type. Usually, I dont think much
about it, I usually returns the interface.

Per example, if I have a methode that returns an ArrayList, I usually
return a List in its signature:

public List getStuffList(){
return new ArrayList();
}

I need to explain that to my collegue (some returns Vector and
ArrayList). I would like to explain them that returning a List is
"better", but I cannot find much argument about it.... Yes, the
implementation can change without changing the signature, but what
else? Could someone gives me a better answer?

Tks

Etienne

 
Reply With Quote
 
 
 
 
Ingo R. Homann
Guest
Posts: n/a
 
      02-26-2007
Hi,

etienno wrote:
> ...
> public List getStuffList(){
> return new ArrayList();
> }
> ...
> Yes, the
> implementation can change without changing the signature


That is the one and only reason! Returning the interface-type prevents
unnecessary dependancies in the code:

Consider, you are returning a "Vector" instead of a "List":

public Vector getStuffList(){
return new Vector();
}

It is possible, that the caller of getStuffList() calls the method
"elementAt()" instead of "get()" on the returned Object:

myObj.getStuffList().elementAt(i)

Now consider, you are refactoring or improving the method
"getStuffList()" because you realise that Vector is synchronized and an
unsynchronized List (e.g. ArrayList) would be better. So, you replace
Vector with ArrayList:

public ArrayList getStuffList(){
return new ArrayList();
}

That would cause the problem that the caller of the method would have to
be changed as well:

myObj.getStuffList().get(i)

If the signature would have been 'List' from the beginning on, then no
changes of the caller would be necessary!

Ciao,
Ingo

 
Reply With Quote
 
 
 
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      02-26-2007
etienno wrote:

> I need to explain that to my collegue (some returns Vector and
> ArrayList). I would like to explain them that returning a List is
> "better", but I cannot find much argument about it.... Yes, the
> implementation can change without changing the signature, but what
> else? Could someone gives me a better answer?


Using the interface instead of the implementation is better,
because of the reason you said. You can change the used
implementation without problem. And changing this happens,
it's not a only a theoretical contruct. Happened here and
will happen at your place as well.

If you change the signature of a method from e.g. java.util.LinkedList
to java.util.ArrayList (or vice versa) all applications using this
class (e.g. residing inside a jar) have to be recompiled otherwise
you will get NoSuchMethodErrors during runtime.

All that can be avoided when using java.util.List. BTW. To
avoid performance-issues it's better to iterate through
a list using

Iterator it myList.iterator();
while (it.hasNext()){
Object o = it.next();
// do something
}

than

for (int i = 0; i < myList.size(); i++){
Object o = myList.get(i);
// do something
}

Especially if you don't know the concrete implementation being
used. LinkedList ist quite slow when doing random access.


Regards, Lothar
--
Lothar Kimmeringer E-Mail:
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
Darryl L. Pierce
Guest
Posts: n/a
 
      02-26-2007
etienno wrote:

> I need to explain that to my collegue (some returns Vector and
> ArrayList). I would like to explain them that returning a List is
> "better", but I cannot find much argument about it.... Yes, the
> implementation can change without changing the signature, but what
> else? Could someone gives me a better answer?


It's partially about simplicity: return only the type that meets your
requirement. Interfaces necessarily meet that guideline.

It's also about refactoring ease: if you lock into ArrayList as the type
your code uses, then you would have to majorly refactor code if you decided
to go with a LinkedList instead for performance reasons. And I would highly
doubt that the requirements are such that you *have* to use an
implementation rather than an interface; ie, since the implementation
details are hidden, coupling your code to a specific implementation makes
the refactor effort again far more difficult when you find yourself needing
to replace ArrayList with LinkedList.

--
Darryl L. Pierce <>
http://mcpierce.dyndns.org/
"What do you care what other people think, Mr. Feynman?"

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-26-2007
etienno wrote:
>
>> I need to explain that to my collegue (some returns Vector and
>> ArrayList). I would like to explain them that returning a List is
>> "better", but I cannot find much argument about it.... Yes, the
>> implementation can change without changing the signature, but what
>> else? Could someone gives me a better answer?


In addition to the other fine answers here, Joshua Bloch has a segment on this
matter in his excellent book, /Effective Java/.

Interface-driven design, or "Inversion of Control", is the basis of patterns
like the Factory, and of frameworks like Spring.

- Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Look up table implemantation using Luts ashu VHDL 1 09-24-2007 01:37 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why use "return (null);" instead of "return null;" ? Carl Java 21 08-24-2006 04:33 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57