Java instanceof interface example

Java instanceof FAQ: How does the Java instanceof operator work with a Java Interface?

The instanceof operator supports inheritance, which I can demonstrate through a simple “instanceof interface” example.

Java instanceof interface example

In the following example, I create a Java interface named Animal, and then create a class named Dog which implements this interface. I'll then test a dog object (which is an instance of the Dog class) against the Animal interface.

Here’s the source code for this Java instanceof interface example:

/**
 * A Java instanceof interface example that
 * demonstrates how instanceof works with a Java interface.
 */
public class JavaInstanceofInterfaceExample
{
  public static void main(String[] args)
  {
    Dog dog = new Dog();

    // basic "instanceof class" test:
    // the dog reference is clearly an instance of the Dog class,
    // so this line will print.
    if (dog instanceof Dog) System.out.println("dog is an instanceof Dog");

    // instanceof interface test:
    // because of java inheritance, the dog reference is also an 
    // instance of the Animal interface, so this line
    // will also print.
    if (dog instanceof Animal) System.out.println("dog is an instanceof Animal");
  }
}

// our example interface
interface Animal {}

// the class that implements our interface
class Dog implements Animal {}

Java instanceof interface - discussion

The output from my “instanceof interface” program looks like this:

dog is an instanceof Dog
dog is an instanceof Animal

The first line of output is very much expected, as the test returns true because the dog object (instance) is an instance of the Dog type:

if (dog instanceof Dog) System.out.println("dog is an instanceof Dog");

That’s the basic instanceof behavior — seeing if a reference is an instance of a particular class.

The second line of output is printed because the dog reference is an instance of the Dog class, and the Dog class implements the Animal interface. This line of code, and the true value it returns, shows Java inheritance at work:

if (dog instanceof Animal) System.out.println("dog is an instanceof Animal");

Java instanceof with multiple levels of inheritance

As you might guess, this instanceof interface behavior works many levels deep, so if you have a Java class that implements an interface, and that interface implements another interface, the object you create from the actual class will be an instance of that class, the interface it implements, and the interface above that.

To demonstrate this, I'll leave you with this second instanceof interface example class:

/**
 * An instanceof example that demonstrates an
 * "instanceof interface" example, i.e., how
 * instanceof works with a Java interface.
 */
public class JavaInstanceofInterfaceExample2
{
  public static void main(String[] args)
  {
    Dog dog = new Dog();
    
    // instanceof object test
    if (dog instanceof Dog) System.out.println("dog is an instanceof Dog");
    
    // instanceof interface test
    if (dog instanceof Animal) System.out.println("dog is an instanceof Animal");
    
    // instanceof interface test #2
    if (dog instanceof LifeForm) System.out.println("dog is an instanceof LifeForm");
  }
}

interface LifeForm {}

interface Animal extends LifeForm {}

class Dog implements Animal {}

Related “Java instanceof” articles

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