Find type of objects at runtime using instanceof operator (java/ example)

  • Given the parent child class hierarchy in java.
  • We would like to compare the instance of classes at run-time.
  • We will use instanceof operator to compare instance of any given class.
    • instanceof operator allows us to detect type of object at run-time
    • The pseudo code to check type of object at run-time is as follows
      • object instanceof type, checks whether object is instance of type.
      • e.g. Integer integer = new Integer(10);
      • integer instanceof Number will returns true (as Integer is sub-class of Number).
  • We can use instanceof operator scenarios like:
    •  If an object is an instance of a class
    • If an object is an instance of a subclass
    • If an object an instance of a class that implements a particular interface.

1. Example: find type of object at runtime using instanceof operator in java.

  • We are create custom objects to compare their instances.
  • We will create couple of concrete class viz Son & Daughter.
    1. Son class extends Parent class (Parent class implicitly extends Object class)
    2. Son class implements Wrestler interface.
    3. Similarly, Daughter class extends Parent class & implement Model interface.
  • We will determine the time of object at run time using instanceof operator.
    • e.g. Parent parent = new Parent();
    • Parent son = new Son();
    • son instanceof Son -> true (son object is Son)
    • son instanceof Parent -> true (Son class extends Parent class)
    • son instanceof Object -> true (Son class extends Parent class & Parent class implicitly extends Object class)
    • son instanceof Wrestler -> true (Son class implements Wrestler interface)
    • son instanceof Model   -> false (Son class does not implements Model interface)
instanceof operator class hierarchy
Fig 1: Class hierarchy

2. Program: find type of object using instanceof operator (java/example)

package org.learn;

class Parent {
}

interface Wrestler {
}

interface Model {
}

class Son extends Parent implements Wrestler {
}

class Daughter extends Parent implements Model {
}


public class DemoInstanceOfOperator {

    public static void main(String[] args) {

        Parent parent = new Parent();
        Parent son = new Son();
        Parent daughter = new Daughter();

        //Check instance of operator on parent
        System.out.println("1. parent instanceof Parent: "+ (parent instanceof Parent));
        System.out.println("2. parent instanceof Son: "+ (parent instanceof Son));
        System.out.println("3. parent instanceof Daughter: " + (parent instanceof Daughter));
        System.out.println("4. parent instanceof Wrestler: "+ (parent instanceof Wrestler));
        System.out.println("5. parent instanceof Model: "+ (parent instanceof Model));
        System.out.println();

        //Apply instanceof operator on son
        System.out.println("6. son instanceof Son: "+ (son instanceof Son));
        System.out.println("7. son instanceof Parent: "+ (son instanceof Parent));
        System.out.println("8. son instanceof Object: "+ (son instanceof Object));
        System.out.println("9. son instanceof Wrestler: "+ (son instanceof Wrestler));
        System.out.println("10. son instanceof Model: "+ (son instanceof Model));
        System.out.println();

        //Apply instanceof operator on daughter
        System.out.println("11. daughter instanceof Parent: " + (daughter instanceof Parent));
        System.out.println("12. daughter instanceof Object: " + (daughter instanceof Object));
        System.out.println("13. daughter instanceof Wrestler: " + (daughter instanceof Wrestler));
        System.out.println("14. daughter instanceof Model: "  + (daughter instanceof Model));
    }
}

3. Output: find type of object using instanceof operator (java/example)

1. parent instanceof Parent: true
2. parent instanceof Son: false
3. parent instanceof Daughter: false
4. parent instanceof Wrestler: false
5. parent instanceof Model: false

6. son instanceof Son: true
7. son instanceof Parent: true
8. son instanceof Object: true
9. son instanceof Wrestler: true
10. son instanceof Model: false

11. daughter instanceof Parent: true
12. daughter instanceof Object: true
13. daughter instanceof Wrestler: false
14. daughter instanceof Model: true
Scroll to Top