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
)

- 50,430
- 22
- 93
- 142

- 1,693
- 4
- 20
- 31
6 Answers
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.

- 267,707
- 33
- 569
- 680
-
Ok, instaceof works. That seems inefficient, though. Writing several methods just to accommodate a small change needed when dealing with different types of organisms. – Conner Ruhl Jan 09 '12 at 22:42
-
1@ConnerRuhl: It sounds like you've discovered the purpose of *polymorphism* and especially *virtual methods*. – Greg Hewgill Jan 09 '12 at 22:46
-
2@ConnerRuhl: That's pretty much the point of polymorphism, though. Any differences in behaviour should be expressed in terms of overriden methods. – Oliver Charlesworth Jan 09 '12 at 22:46
-
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. – Conner Ruhl Jan 09 '12 at 23:00
-
2@ConnerRuhl: Hopefully not. You should find a way to express only the differences as an overriden method. See e.g. the [*template pattern*](http://en.wikipedia.org/wiki/Template_pattern). – Oliver Charlesworth Jan 09 '12 at 23:06
-
Nobody said that the road to virtue is easy. – Mike Nakis Jan 09 '12 at 23:36
-
@ConnerRuhl Relying solely on polymorphism to express different behaviour is not mandatory, and depending on the situation using instanceof (or some other determiner) is often necessary, so do not feel compelled to rewrite your entire application (but do consider if a polymorphic approach would be better). Perhaps you can have the 75 lines you mention exist in one shared method that defers to a polymorphic method of the object to handle the functionality of the one line of code that differs. – Trevor Freeman Jan 10 '12 at 00:30
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.

- 56,297
- 11
- 110
- 142
-
@cHao I updated my answer with the "and _not_ a descendant thereof". Thank you. – Mike Nakis Jan 09 '12 at 22:43
Java has an instanceof
operator. However, that type of thing can be contrary to object-oriented design.

- 5,535
- 2
- 25
- 38
-
1+ for "that type of thing can be contrary to object-oriented design." – Hovercraft Full Of Eels Jan 09 '12 at 22:45
-
1I feel like up-voting for your username being based on my favorite MP phrase. – jbindel Jan 10 '12 at 02:56
-
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. – jbindel Jan 10 '12 at 02:57
You can use the instanceof
operator

- 6,397
- 2
- 41
- 52
-
-
`final Organism organism = new Carnivore(); if(organism instanceof Carnivore){ .... }` – asenovm Jan 09 '12 at 22:39
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.

- 1,024
- 3
- 11
- 30
objInstance instanceof Carnivore
. Here objInstance is the object you want to test.

- 65,990
- 13
- 130
- 167