• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to pass a child list to a parentlist  RSS feed

 
chaohua wang
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Folks,

I have a
cat class , its super class is animal class.

cat extends animal{

}
i got a cat list:

List<cat> catlist= task.getCatList();

I need to pass this catlist to a method:

public takeCareOfAllanimals (List<animal> animals).


I am not sure how to do this? I tied to cast catlist to (animal)catlist, but it doesn't allow me.

thank you

Chwang
 
Paul Clapham
Sheriff
Posts: 22821
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't do that. Here's why:

So, no. List<Cat> is not a subtype of List<Animal>. you should be doing this:
 
chaohua wang
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
I see, Thank you very much for the excellent explanation.

Best Regards,
Chwnag
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look through the Java™ Tutorials; there are two sections on generics, which should help you. Also Google for Angelika Langer Java Generics FAQ.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!