instanceof Keyword in Java (original) (raw)

The instanceof keyword is used to check whether an object belongs to a specific class, subclass, or interface. It compares an object reference with a type and returns a boolean value (true or false). This keyword is commonly used to verify an object's type before performing typecasting, helping to avoid runtime errors such as ClassCastException.

Syntax

objectReference instanceof ClassName

Java `

// Importing required I/O classes import java.io.*;

// Main class class GFG { public static void main(String[] args) {

    // Creating object of class inside main()
    GFG object = new GFG();

    // Returning instanceof
    System.out.println(object instanceof GFG);
}

}

`

**Explanation: This program creates an object of the GFG class and checks whether it belongs to the same class using the instanceof keyword. Since the object is created from GFG, the condition returns true.

Examples of Java instance of keyword

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

**Example: Reating sample classes with a parent-child relationship.

Java `

// Parent class class Parent { }

// Class 2 // Child class class Child extends Parent { }

// Class 3 // Main class class GFG {

// Main driver method
public static void main(String[] args)
{

    // Creating object of child class
    Child cobj = new Child();

    // A simple case
    if (cobj instanceof Child)
        System.out.println("cobj is instance of Child");
    else
        System.out.println(
            "cobj is NOT instance of Child");

    // instanceof returning true for Parent class also
    if (cobj instanceof Parent)
        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 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

**Explanation: A Child object is created and tested against Child, Parent, and Object classes. Since a child object inherits from its parent and every class inherits from Object, all checks return true.

**Example: Instanceof returning false for null

Java `

class Test { }

class Main { public static void main(String[] args) { Test tobj = null;

    // A simple case
    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

**Explanation: The reference variable tobj is assigned null and checked using instanceof. Since null does not refer to any object, the expression returns false

**Example: Parent object is not an instance of Child

Java `

class Parent { } class Child extends Parent { }

// Driver Class class Test { // main function 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

**Explanation: A Parent object is created and checked whether it is an instance of Child. Since a parent object cannot be treated as a child object, the result is false.

**Example: Parent reference referring to a Child is an instance of a Child

Java `

class Parent { } class Child extends Parent { }

// Driver class class Test { // main function public static void main(String[] args) { // Reference is Parent type but object is // of child type. 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

**Explanation: A parent reference variable stores a Child object. Although the reference type is Parent, the actual object is Child, so instanceof Child returns true.

**Example: Java program to demonstrate that non-method members are accessed according to reference type (Unlike methods which are accessed according to the referred object)

Java `

class Parent { int value = 1000; }

class Child extends Parent { int value = 10; }

// Driver class class Test { public static void main(String[] args) { Parent cobj = new Child(); Parent par = cobj;

    // Using instanceof to make sure that par
    // is a valid reference before typecasting
    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

Application of Java instance of keyword