Java ‘instanceof’ inheritance examples

In other tutorials I’ve written about the Java “instanceof null” behavior, and the Java “instanceof interface” behavior, and it occurred to me that I’ve never written anything about how the instanceof operator works with Java class inheritance.

Java instanceof inheritance example

To that end, here’s the source code for a complete Java class that demonstrates how instanceof works with Java class inheritance. To make things easy, I named the parent class Parent, and the child class which inherits from the Parent class is named Child:

/**
 * Several different Java instanceof class examples,
 * in particular, showing how the instanceof operator works with
 * a Java class and subclass.
 * @author Alvin Alexander, http://alvinalexander.com
 */
public class JavaInstanceofInheritanceExample
{
  public static void main(String[] args)
  {
    Parent parent = new Parent();
    Child child = new Child();
    
    // parent is an instanceof class Parent, so this line will print
    if (parent instanceof Parent) System.out.println("parent is instanceof Parent class");

    // child is an instanceof class Child, so this line will print
    if (child instanceof Child) System.out.println("child is instanceof Child class");

    // because of inheritance (Child extends from Parent), 
    // child is also an instanceof class Parent, so this line will print
    if (child instanceof Parent) System.out.println("child is instanceof Parent class");
    
    // taking it to the extreme, because Child extends Parent which
    // implicitly extends Object, child is an instanceof Object,
    // so this line also prints
    if (parent instanceof Object) System.out.println("parent is instanceof Object class");
  }
}

// sample parent class
class Parent {} 

// sample child class
class Child extends Parent {}

Example program output

My instanceof example class prints the following output:

parent is instanceof Parent class
child is instanceof Child class
child is instanceof Parent class
parent is instanceof Object class

This first line of output is printed because the parent instance is clearly an “instance of” the class Parent:

parent is instanceof Parent class

The second line is printed for precisely the same reason (the child reference is an “instance of” the Child class):

child is instanceof Child class

The third line of output is printed because of inheritance, and how the instanceof operator works with class inheritance. Put in words, “the child reference is an instance of the Child class, and the Child class extends the Parent class, so child is also an instance of Parent.” Here’s that line of output:

child is instanceof Parent class

To drive home this instanceof/inheritance point, the fourth test shows that the child reference is also an instance of the Java Object base class, as you can see from the last line of output:

parent is instanceof Object class

In short, the Child class is a child of the Parent class, and Parent (like all other Java objects) inherits from the base Java Object class.

Related Java “instanceof” tutorials

Here are links to some of my other Java instanceof tutorials: