instanceof – Know Object Type in Java


instanceof operation in java

In Java, instanceof operator is used to check the type of an object at run-time.   instanceof operator is important in case of down-casting object at run-time. instanceof operator return boolean value, if an object reference is of specified type then it return true otherwise false.

Simple rule is “instanceof” would return true for all the “IS-A” relationship.

Let us take an example,

abstract class Animal { public abstract void eat();}          // Parent class

class Cow extends Animal { public void eat(){……} // child1 implements eat()

                              public void run(){……} }

class Tiger extends Animal { public void eat(){……} // child2 implements eat()

public void hunt(){……} }

Animal jimmy = new Cow(); // jimmy reference variable refer Cow object. (parent reference variable refer child object)

 

if(jimmy instanceof Animal)            // jimmy    “IS-A” Animal and it would return true
if(jimmy instanceof Cow)                 // jimmy    “IS-A” Cow and it would return true
if(jimmy instanceof Tiger)                // jimmy     is not a Tiger and it would return false.
if(jimmy instanceof Object)             // All the objects in java is internally subclass of “Object” by default. It returns true.

 

Cow, Tiger and Animal classes are in inheritance relationship . we can access all the methods of Animal using jimmy reference variable, but we can’t access the Cow’s specific method such as run().    Here jimmy is actually Cow object and it should able to access run.   To access the run() method we have to downcast the “jimmy” as ((Cow) jimmy).    Using this down-casting we can access the run method.

((Cow) jimmy).run() //Downcasting and access the Cow’s specific method.

ClassCastException:

Class cast exception thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.   Down-casting without checking actual type of object would throw classcastexception at run time. Use can write code like

((Tiger) jimmy).hunt(); //Down-casting the Animal to Tiger which is actually Cow. This would compile and throw run-time exception.

This would not throw compile time error, but run-time class cast exception would be thrown. To avoid class cast exception, better to check the object type by “instanceof” operator and downcast the type.

if (jimmy instanceof Cow){

((Cow) jimmy).run();

}

if (bakira instanceof Tiger){

((Tiger) bakira).hunt()

}

instanceof” operation is helpful to avoid classcastexception during down-casting the object type. Few people may argue that why Cow object referred by Animal reference type. Why can’t directly refer the Cow object by Cow reference type like below,

Animal jimmy = new Cow();             // Few programmers feel this is unnecessary

Cow jimmy = new Cow();                 // and feel that direct reference would be better way and good option.

But access the objects by parent reference variable will be great way to achieve high level abstraction in architecture . Curious to understand this? Let us discuss more on next section “polymorphism” for high level  abstraction and down-casting. Stay tuned!!!!

You may also like