I have three classes (Carnivore, Herbivore, and Plant) that extend another class (Organism). How can I tell which subclass an object is a part of? So far I have a property that has the classes’ name, but I think it could be possible to use an operator similar to javascript’s typeof. (Similar to: Organism typeof Carnivore)
I have three classes ( Carnivore , Herbivore , and Plant ) that extend
Share
You can use the
instanceofkeyword.Note, however, that needing to use this is often a sign of a bad design. You should typically write method overrides in each of your derived classes so that you don’t explicitly need to check which class something is.