• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

how to pass a child list to a parentlist

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 27536
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,
I see, Thank you very much for the excellent explanation.

Best Regards,
Chwnag
 
Marshal
Posts: 77249
371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic