Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Casting between typed Lists?

Thread view: 
jagonzal@gmail.com - 18 Aug 2006 22:14 GMT
Hi,

I have an Interface B, which extends Interface A.

I have a method whose signature is:

public void doSomething(List<A> list){
...
}

However, when I try to invoke it like this:

List<B> list2 = new ArrayList<B>();
doSomething(list2);

the compiler complains that it can't take a List<B> instead of a
List<A>. I say that, theoretically, it should work, since a B element
is also a A element due to interface inheritance. Casting a B variable
to A does not produce a ClassCastException. However, the compiler says
that it doesn't.

Is there any way to go around this (Other than working with non-typed
lists)? (casting the list? I tried that but it doesn't work :)

TIA,

JGN.
Oliver Wong - 18 Aug 2006 22:33 GMT
> Hi,
>
[quoted text clipped - 19 lines]
> Is there any way to go around this (Other than working with non-typed
> lists)? (casting the list? I tried that but it doesn't work :)

   Instead of "A" and "B", let's call them "Vehicule" and "Car". Car
extends Vehicule.

   The doSomething(List<Vehicule> list)  method does something to a list of
vehicules. One of the things it might do is add more vehicules to the list.
For example, it might add some boats to the list.

   If you pass in a List<Car> instead of a List<Vehicule>, you'll get an
problems, because the doSomething method will try to add a Boat to a
List<Car>, and obvious a Boat is not a Car.

   Solution A: change the method signature from doSomething(List<A> list)
to doSomething(List<? extends A> list).

   Solution B: change the object declaration from List<B> list2 = new
ArrayList<B>() to List<A> list2 = new ArrayList<A>().

Which solution is better depends entirely on what it is your program is
supposed to do.

   - Oliver
jagonzal@gmail.com - 18 Aug 2006 22:47 GMT
>     The doSomething(List<Vehicule> list)  method does something to a list of
> vehicules. One of the things it might do is add more vehicules to the list.
[quoted text clipped - 3 lines]
> problems, because the doSomething method will try to add a Boat to a
> List<Car>, and obvious a Boat is not a Car.

Ah, I had not considered this scenario - thank you :)

>     Solution A: change the method signature from doSomething(List<A> list)
> to doSomething(List<? extends A> list).

I think I'll go with this - I'm getting the list from another object,
and I can't change that return type.

> Which solution is better depends entirely on what it is your program is
> supposed to do.
>
>     - Oliver

Thank you very much :)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.