• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get a List of specific types from a List of base class types  RSS feed

 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say I have a class Animal. Dog, Cat, Bird all extend from the Animal class.

Now lets say I have a class that houses those animals like a kennel. Is it possible to ask for a List of all Cats in a generic way?

 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, but you would have to pass the type as a parameter of the method, something like this:

Then you could iterate through the "animals" list and for each animal, see if its class was compatible with that parameter. You might use Class.isAssignableFrom(select) for that.
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This can also be used:

and call it as:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!