Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Inheritance with typed collections

Reply
Thread Tools

Inheritance with typed collections

 
 
Will Cheeder
Guest
Posts: n/a
 
      06-20-2007
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

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      06-20-2007
Will Cheeder <(E-Mail Removed)> 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 );


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-21-2007
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      06-21-2007
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....bd48d4566a7575


Phil
 
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
mix statically typed with dynamically typed Yingjie Lan Python 4 01-29-2010 08:50 AM
Strongly Typed Collections Dave T ASP .Net 1 03-17-2008 09:51 PM
special needs with Stongly-typed collections? ECathell ASP .Net 2 06-20-2005 06:10 PM
How can I add a row from a typed datatable to another instance of that typed datatable? Ersin Gençtürk ASP .Net 1 10-06-2004 01:11 PM
Traditional Collections and Objects or Typed Datasets ? Rob Thomas ASP .Net 3 08-26-2003 09:06 PM



Advertisments