I have three classes (Carnivore
, Herbivore
, and Plant
) that extend another class (Organism
). How can I tell which subclass an
You can say if( animal instanceof Carnivore )
to find out if it is a Carnivore or a descendant thereof, and you can use if( animal.getClass() == Carnivore.class )
to find out if it is exactly a Carnivore and not a descendant thereof.
However, the fact that you need to perform a check of this kind usually means that you have a flaw in your design, a missing overridable method, or something like that.