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 / June 2007

Tip: Looking for answers? Try searching our database.

Inheritance with typed collections

Thread view: 
Will Cheeder - 20 Jun 2007 23:26 GMT
Hi,

If I have a class, Cat, which extends Animal, why can't I pass a
List<Cat> to a method that takes a List<Animal>? This should work,
since Cat inherits from Animal. Why doesn't inheritance include typed
collections? This seems like a gap in Java's polymorphism.

Thanks,

Andrew
Stefan Ram - 20 Jun 2007 23:30 GMT
>why can't I pass a List<Cat> to a method that takes a List<Animal>?

 Because an list of animals can do more than a list
 of cats (i.e., accept an animal), so that a list
 of cats is not a subtype of it.

 Assume,

java.util.List<Cat> catList = new java.util.ArrayList<Cat>();

 Now assume, the following assignement would be allowed:

java.util.List<animal> animalList = catList;

 Then one could add an animal to this »animalList«:

animalList.add( new animal(){} );

 However, this would break the type of »catList«, because
 the following call to the get-Operation of the catList now
 will not return a cat, because we have been allowed to add
 an animal above:

Cat cat = catList.get( 0 );
Eric Sosman - 21 Jun 2007 02:45 GMT
> Hi,
>
> If I have a class, Cat, which extends Animal, why can't I pass a
> List<Cat> to a method that takes a List<Animal>? This should work,
> since Cat inherits from Animal. Why doesn't inheritance include typed
> collections? This seems like a gap in Java's polymorphism.

    Because your method might add a Dog to the List<Animal>.
Since it's actually a List<Cat>, you would then have trouble.

    The Hydrogen Dog and the Cobalt Cat
    Side by side in the armory sat.
    Nobody talked about fusion or fission;
    Everyone spoke of their peacetime mission
    'Til somebody came in and opened the door ...

    There they were in a neutron fog:
    The Codrogen Cat and the Hybalt Dog.
    They mushroomed up with a terrible roar,
    And nobody never was there, no more.

    -- "A Space Child's Mother Goose"
    Winsor & Parry
    (from possibly inaccurate memory)

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Philipp - 21 Jun 2007 06:54 GMT
Will Cheeder a écrit :
> Hi,
>
> If I have a class, Cat, which extends Animal, why can't I pass a
> List<Cat> to a method that takes a List<Animal>? This should work,
> since Cat inherits from Animal. Why doesn't inheritance include typed
> collections? This seems like a gap in Java's polymorphism.

Make the method parameter signature be

public void myMethod(List<? extends Animal> myAnimalList)

and it will work.

See also for this thread for links to tutorials I found useful
http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/bc
bd48d4566a7575


Phil


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



©2009 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.