1

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)

6 Answers 6

3

You can use the instanceof keyword.

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Ok, instaceof works. That seems inefficient, though. Writing several methods just to accommodate a small change needed when dealing with different types of organisms.
@ConnerRuhl: It sounds like you've discovered the purpose of polymorphism and especially virtual methods.
@ConnerRuhl: That's pretty much the point of polymorphism, though. Any differences in behaviour should be expressed in terms of overriden methods.
So I have to override a method with 75 lines of code just to make one line of my code differ for each type of organism? It just seems so clunky.
@ConnerRuhl: Hopefully not. You should find a way to express only the differences as an overriden method. See e.g. the template pattern.
|
2

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.

1 Comment

@cHao I updated my answer with the "and not a descendant thereof". Thank you.
1

Java has an instanceof operator. However, that type of thing can be contrary to object-oriented design.

3 Comments

1+ for "that type of thing can be contrary to object-oriented design."
I feel like up-voting for your username being based on my favorite MP phrase.
Oli's answer also notes this, and in general, an good object oriented design would use polymorphism such that the difference in behavior is in each subclass, and not in some controller class that is checking the types of the objects.
0

You can use the instanceof operator

2 Comments

if (Organism instanceof Carnivore) {}
final Organism organism = new Carnivore(); if(organism instanceof Carnivore){ .... }
0

objInstance instanceof Carnivore. Here objInstance is the object you want to test.

Comments

0

Take a look at instanceof operator

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

Note that although many people thinks that using it may be considered dangerous, they even compare to GOTO, but it's not bad in some cases. You can use it, but not really often.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.