instanceof Keyword in Java (original) (raw)

Last Updated : 06 Jul, 2023

In Java, instanceof is a keyword used for checking if a reference variable contains a given type of object reference or not. Following is a Java program to show different behaviors of instanceof. Henceforth it is known as a comparison operator where the **instance is getting compared to **type returning boolean true or false as in Java we do not have 0 and 1 boolean return types.

**Example of the Java instanceof Keyword

Java

import java.io.*;

class GFG {

`` public static void main(String[] args)

`` {

`` GFG object = new GFG();

`` System.out.println(object instanceof GFG);

`` }

}

Examples of Java instaceof keyword

Here are all the examples of instanceof keywords with their respective cases:

1. Here we will be creating sample classes with a parent-child relationship.

Java

class Parent {

}

class Child extends Parent {

}

class GFG {

`` public static void main(String[] args)

`` {

`` Child cobj = new Child();

`` if (cobj instanceof Child)

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

`` else

`` System.out.println(

`` "cobj is NOT instance of Child" );

`` if (cobj instanceof Parent)

`` System.out.println(

`` "cobj is instance of Parent" );

`` else

`` System.out.println(

`` "cobj is NOT instance of Parent" );

`` if (cobj instanceof Object)

`` 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

2. instanceof returning false for null

Java

class Test {

}

class Main {

`` public static void main(String[] args)

`` {

`` Test tobj = null ;

`` if (tobj instanceof Test)

`` 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

3. Parent object is not an instance of Child

Java

class Parent {

}

class Child extends Parent {

}

class Test {

`` public static void main(String[] args)

`` {

`` Parent pobj = new Parent();

`` if (pobj instanceof Child)

`` 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

4. Parent reference referring to a Child is an instance of a Child

Java

class Parent {

}

class Child extends Parent {

}

class Test {

`` public static void main(String[] args)

`` {

`` Parent cobj = new Child();

`` if (cobj instanceof Child)

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

`` else

`` System.out.println(

`` "cobj is NOT instance of Child" );

`` }

}

Output

cobj is instance of Child

**Application of Java instanceof keyword

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 members using typecasting.

**Syntax

((child_class_name) Parent_Reference_variable).func.name()

When we do typecasting, 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 instanceof, then do typecasting.

**Example

Java

class Parent {

`` int value = 1000 ;

}

class Child extends Parent {

`` int value = 10 ;

}

class Test {

`` public static void main(String[] args)

`` {

`` Parent cobj = new Child();

`` Parent par = cobj;

`` if (par instanceof Child) {

`` System.out.println(

`` "Value accessed through "

`` + "parent reference with typecasting is "

`` + ((Child)par).value);

`` }

`` }

}

Output

Value accessed through parent reference with typecasting is 10