Java instanceof and its applications

instanceof is a keyword that is used for checking if a reference variable is containing a given type of object reference or not.

Following is a Java program to show different behaviors of instanceof.

// Java program to demonstrate working of instanceof

// Creating sample classes with parent Child

// relationship

classParent { }

classChild extendsParent { }

classTest

{

publicstaticvoidmain(String[] args)

{

Child cobj = newChild();

// A simple case

if(cobj instanceofChild)

System.out.println("cobj is instance of Child");

else

System.out.println("cobj is NOT instance of Child");

// instanceof returns true for Parent class also

if(cobj instanceofParent)

System.out.println("cobj is instance of Parent");

else

System.out.println("cobj is NOT instance of Parent");

// instanceof returns true for all ancestors (Note : Object

// is ancestor of all classes in Java)

if(cobj instanceofObject)

System.out.println("cobj is instance of Object");

else

System.out.println("cobj is NOT instance of Object");

}

}

Output :

cobj is instance of Child
cobj is instance of Parent
cobj is instance of Object

instanceof returns false for null

// Java program to demonstrate that instanceof

// returns false for null

classTest { }

classMain

{

publicstaticvoidmain(String[] args)

{

Test tobj = null;

// A simple case

if(tobj instanceofTest)

System.out.println("tobj is instance of Test");

else

System.out.println("tobj is NOT instance of Test");

}

}

Output :

tobj is NOT instance of Test

A parent object is not an instance of Child

// A Java program to show that a parent object is

// not an instance of Child

classParent { }

classChild extendsParent { }

classTest

{

publicstaticvoidmain(String[] args)

{

Parent pobj = newParent();

if(pobj instanceofChild)

System.out.println("pobj is instance of Child");

else

System.out.println("pobj is NOT instance of Child");

}

}

Output :

pobj is NOT instance of Child

A parent reference referring to a Child is an instance of Child

// A Java program to show that a parent reference

// referring to a Child is an instance of Child

classParent { }

classChild extendsParent { }

classTest

{

publicstaticvoidmain(String[] args)

{

// Reference is Parent type but object is

// of child type.

Parent cobj = newChild();

if(cobj instanceofChild)

System.out.println("cobj is instance of Child");

else

System.out.println("cobj is NOT instance of Child");

}

}

Output :

cobj is an instance of Child

Application:
We have seen here that a parent class data member is accessed when a reference of parent type refers to a child object. We can access child data member using type casting.

Syntax: (child_class_name) Parent_Reference_variable).func.name().

When we do typecast, it is always a good idea to check if the typecasting is valid or not. instanceof helps us here. We can always first check for validity using instancef, then do typecasting.

// A Java program to demonstrate that non-method

// members are accessed according to reference

// type (Unlike methods which are accessed according

// to the referred object)

classParent

{

intvalue = 1000;

}

classChild extendsParent

{

intvalue = 10;

}

// Driver class

classTest

{

publicstaticvoidmain(String[] args)

{

Parent cobj = newChild();

Parent par = cobj;

// Using instanceof to make sure that par

// is a valid reference before typecasting

if(par instanceofChild)

{

System.out.println("Value accessed through "+

"parent reference with typecasting is "+

((Child)par).value);

}

}

}

Output:

Value accessed through parent reference with typecasting is 10

This article is contributed by Twinkle Tyagi .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.