I have the following classes:
Class Dog extends Animal
Class Cat extends Animal
Class Mouse extends Animal
Furthermore, I have a List
in which I put every Animal
, regardless of the subclass:
List<? extends Animal> list = new ArrayList<? extends Animal>();
Populating said list with Dogs
, Cats
and Mice
works perfectly, unless I want to later check what kind of Animal
there is inside the list.
for (Animal animal: list) {
if (animal instanceof Dog) {
//do some dog stuff
}
if (animal instanceof Cat) {
// do some cat stuff
}
}
How can I check for each Animal in the list whether it is a Dog, a Cat or a Mouse?