if (animal instanceof Herbivore)
{
this.herbivores.add((Herbivore) animal);
}
else if (animal instanceof Carnivore)
{
this.carnivores.add((Carnivore) animal);
}
In the code snippet above, the animal variable is an Animal object, which is the superclass for subclasses, Herbivore and Carnivore. Here, I want to check if the animal is a subclass or not, and if so, add it to the respective ArrayList of Carnivores or Herbivores. Is this bad practice; I heard that downcasting is always bad. If so, what's a better solution?