Inheritance with typed collections

Discussion in 'Java' started by Will Cheeder, Jun 20, 2007.

  1. Will Cheeder

    Will Cheeder Guest

    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
     
    Will Cheeder, Jun 20, 2007
    #1
    1. Advertising

  2. Will Cheeder

    Stefan Ram Guest

    Will Cheeder <> writes:
    >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 );
     
    Stefan Ram, Jun 20, 2007
    #2
    1. Advertising

  3. Will Cheeder

    Eric Sosman Guest

    Will Cheeder wrote:
    > 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)

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 21, 2007
    #3
  4. Will Cheeder

    Philipp Guest

    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/bcbd48d4566a7575


    Phil
     
    Philipp, Jun 21, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Thomas
    Replies:
    3
    Views:
    385
  2. ECathell
    Replies:
    2
    Views:
    339
    Steve C. Orr [MVP, MCSD]
    Jun 20, 2005
  3. Doug Poland
    Replies:
    9
    Views:
    752
    VisionSet
    Sep 27, 2003
  4. Dave T

    Strongly Typed Collections

    Dave T, Mar 17, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    422
    sloan
    Mar 17, 2008
  5. mutex
    Replies:
    0
    Views:
    227
    mutex
    Jul 27, 2003
Loading...

Share This Page