Introduction
In this lab, you will learn how to check if an object is an instance of a specific class or interface in Java using the instanceof keyword. We will explore its basic usage, test its behavior with subclasses, and understand how it handles null objects.
Through hands-on examples, you will gain practical experience in using instanceof to perform runtime type checking, a fundamental concept in object-oriented programming.
Use instanceof for Class Check
In this step, we will explore the instanceof keyword in Java. The instanceof keyword is used to test whether an object is an instance of a particular class or implements a particular interface. It's a useful tool for checking the type of an object at runtime.
Let's create a simple Java program to demonstrate how instanceof works.
Open the WebIDE and make sure you are in the
~/projectdirectory. You can confirm this by looking at the terminal prompt or by typingpwdand pressing Enter.Create a new Java file named
TypeCheck.javain the~/projectdirectory. You can do this by right-clicking in the File Explorer on the left and selecting "New File", then typingTypeCheck.java.Open the
TypeCheck.javafile in the editor and paste the following code:class Animal { // Base class } class Dog extends Animal { // Subclass of Animal } class Cat extends Animal { // Subclass of Animal } public class TypeCheck { public static void main(String[] args) { Animal myAnimal = new Dog(); // Check if myAnimal is an instance of Dog if (myAnimal instanceof Dog) { System.out.println("myAnimal is an instance of Dog"); } else { System.out.println("myAnimal is not an instance of Dog"); } // Check if myAnimal is an instance of Cat if (myAnimal instanceof Cat) { System.out.println("myAnimal is an instance of Cat"); } else { System.out.println("myAnimal is not an instance of Cat"); } // Check if myAnimal is an instance of Animal if (myAnimal instanceof Animal) { System.out.println("myAnimal is an instance of Animal"); } else { System.out.println("myAnimal is not an instance of Animal"); } } }In this code:
- We define a base class
Animaland two subclassesDogandCat. - In the
mainmethod, we create anAnimalvariablemyAnimaland assign aDogobject to it. This is possible because aDogis a type ofAnimal. - We then use the
instanceofkeyword to check ifmyAnimalis an instance ofDog,Cat, andAnimal.
- We define a base class
Save the
TypeCheck.javafile (Ctrl+S or Cmd+S).Compile the Java program by opening the terminal at the bottom of the WebIDE and running the following command:
javac TypeCheck.javaIf there are no errors, this command will create a
TypeCheck.class,Animal.class,Dog.class, andCat.classfile in the~/projectdirectory.Run the compiled program using the following command:
java TypeCheckYou should see output similar to this:
myAnimal is an instance of Dog myAnimal is not an instance of Cat myAnimal is an instance of AnimalThis output confirms that
myAnimal, which holds aDogobject, is indeed an instance ofDogand also an instance of its superclassAnimal, but not an instance ofCat.
You have successfully used the instanceof keyword to check the type of an object in Java. In the next step, we will explore how instanceof behaves with subclasses.
Test with Subclasses
In the previous step, we saw that an object is considered an instance of its own class and also an instance of its superclass. Let's further explore how instanceof works with different object types and their subclasses.
We will modify our existing TypeCheck.java file to test instanceof with a Cat object.
Open the
TypeCheck.javafile in the WebIDE editor.Locate the
mainmethod and add the following lines of code after the existing checks formyAnimal:System.out.println("\n--- Testing with a Cat object ---"); Animal anotherAnimal = new Cat(); // Check if anotherAnimal is an instance of Dog if (anotherAnimal instanceof Dog) { System.out.println("anotherAnimal is an instance of Dog"); } else { System.out.println("anotherAnimal is not an instance of Dog"); } // Check if anotherAnimal is an instance of Cat if (anotherAnimal instanceof Cat) { System.out.println("anotherAnimal is an instance of Cat"); } else { System.out.println("anotherAnimal is not an instance of Cat"); } // Check if anotherAnimal is an instance of Animal if (anotherAnimal instanceof Animal) { System.out.println("anotherAnimal is an instance of Animal"); } else { System.out.println("anotherAnimal is not an instance of Animal"); }Your complete
TypeCheck.javafile should now look like this:class Animal { // Base class } class Dog extends Animal { // Subclass of Animal } class Cat extends Animal { // Subclass of Animal } public class TypeCheck { public static void main(String[] args) { Animal myAnimal = new Dog(); // Check if myAnimal is an instance of Dog if (myAnimal instanceof Dog) { System.out.println("myAnimal is an instance of Dog"); } else { System.out.println("myAnimal is not an instance of Dog"); } // Check if myAnimal is an instance of Cat if (myAnimal instanceof Cat) { System.out.println("myAnimal is an instance of Cat"); } else { System.out.println("myAnimal is not an instance of Cat"); } // Check if myAnimal is an instance of Animal if (myAnimal instanceof Animal) { System.out.println("myAnimal is an instance of Animal"); } else { System.out.println("myAnimal is not an instance of Animal"); } System.out.println("\n--- Testing with a Cat object ---"); Animal anotherAnimal = new Cat(); // Check if anotherAnimal is an instance of Dog if (anotherAnimal instanceof Dog) { System.out.println("anotherAnimal is an instance of Dog"); } else { System.out.println("anotherAnimal is not an instance of Dog"); } // Check if anotherAnimal is an instance of Cat if (anotherAnimal instanceof Cat) { System.out.println("anotherAnimal is an instance of Cat"); } else { System.out.println("anotherAnimal is not an instance of Cat"); } // Check if anotherAnimal is an instance of Animal if (anotherAnimal instanceof Animal) { System.out.println("anotherAnimal is an instance of Animal"); } else { System.out.println("anotherAnimal is not an instance of Animal"); } } }Save the modified
TypeCheck.javafile.Compile the program again in the terminal:
javac TypeCheck.javaRun the compiled program:
java TypeCheckYou should now see output similar to this:
myAnimal is an instance of Dog myAnimal is not an instance of Cat myAnimal is an instance of Animal --- Testing with a Cat object --- anotherAnimal is not an instance of Dog anotherAnimal is an instance of Cat anotherAnimal is an instance of AnimalThis output shows that
anotherAnimal, which holds aCatobject, is an instance ofCatandAnimal, but notDog. This reinforces the concept thatinstanceofchecks the actual type of the object and its inheritance hierarchy.
You have successfully tested the instanceof keyword with different subclass objects. In the next step, we will see how instanceof behaves with null objects.
Verify with Null Objects
In this final step, we will investigate how the instanceof keyword behaves when the object being tested is null. Understanding this is important to avoid potential errors in your Java programs.
We will again modify our TypeCheck.java file to include a test with a null object.
Open the
TypeCheck.javafile in the WebIDE editor.Add the following lines of code at the end of the
mainmethod, after the previous tests:System.out.println("\n--- Testing with a null object ---"); Animal nullAnimal = null; // Check if nullAnimal is an instance of Dog if (nullAnimal instanceof Dog) { System.out.println("nullAnimal is an instance of Dog"); } else { System.out.println("nullAnimal is not an instance of Dog"); } // Check if nullAnimal is an instance of Animal if (nullAnimal instanceof Animal) { System.out.println("nullAnimal is an instance of Animal"); } else { System.out.println("nullAnimal is not an instance of Animal"); }Your complete
TypeCheck.javafile should now look like this:class Animal { // Base class } class Dog extends Animal { // Subclass of Animal } class Cat extends Animal { // Subclass of Animal } public class TypeCheck { public static void main(String[] args) { Animal myAnimal = new Dog(); // Check if myAnimal is an instance of Dog if (myAnimal instanceof Dog) { System.out.println("myAnimal is an instance of Dog"); } else { System.out.println("myAnimal is not an instance of Dog"); } // Check if myAnimal is an instance of Cat if (myAnimal instanceof Cat) { System.out.println("myAnimal is not an instance of Cat"); } else { System.out.println("myAnimal is not an instance of Cat"); } // Check if myAnimal is an instance of Animal if (myAnimal instanceof Animal) { System.out.println("myAnimal is an instance of Animal"); } else { System.out.println("myAnimal is not an instance of Animal"); } System.out.println("\n--- Testing with a Cat object ---"); Animal anotherAnimal = new Cat(); // Check if anotherAnimal is an instance of Dog if (anotherAnimal instanceof Dog) { System.out.println("anotherAnimal is not an instance of Dog"); } else { System.out.println("anotherAnimal is not an instance of Dog"); } // Check if anotherAnimal is an instance of Cat if (anotherAnimal instanceof Cat) { System.out.println("anotherAnimal is an instance of Cat"); } else { System.out.println("anotherAnimal is an instance of Cat"); } // Check if anotherAnimal is an instance of Animal if (anotherAnimal instanceof Animal) { System.out.println("anotherAnimal is an instance of Animal"); } else { System.out.println("anotherAnimal is an instance of Animal"); } System.out.println("\n--- Testing with a null object ---"); Animal nullAnimal = null; // Check if nullAnimal is an instance of Dog if (nullAnimal instanceof Dog) { System.out.println("nullAnimal is an instance of Dog"); } else { System.out.println("nullAnimal is not an instance of Dog"); } // Check if nullAnimal is an instance of Animal if (nullAnimal instanceof Animal) { System.out.println("nullAnimal is an instance of Animal"); } else { System.out.println("nullAnimal is not an instance of Animal"); } } }Save the modified
TypeCheck.javafile.Compile the program in the terminal:
javac TypeCheck.javaRun the compiled program:
java TypeCheckYou should see output similar to this:
myAnimal is an instance of Dog myAnimal is not an instance of Cat myAnimal is an instance of Animal --- Testing with a Cat object --- anotherAnimal is not an instance of Dog anotherAnimal is an instance of Cat anotherAnimal is an instance of Animal --- Testing with a null object --- nullAnimal is not an instance of Dog nullAnimal is not an instance of AnimalAs you can see from the output, when the object being tested with
instanceofisnull, the result is alwaysfalse. This is a crucial point to remember when usinginstanceofto avoid unexpected behavior orNullPointerExceptions.
You have successfully verified how the instanceof keyword behaves with null objects. This concludes our exploration of the instanceof keyword.
Summary
In this lab, we learned how to use the instanceof keyword in Java to check if an object is an instance of a specific class or implements a particular interface. We demonstrated its usage by creating a simple program with a base class and subclasses, and then using instanceof to verify the type of an object at runtime.
We further explored how instanceof behaves with subclasses, confirming that an object is considered an instance of its own class as well as any superclasses. Finally, we examined the behavior of instanceof when applied to null objects, understanding that it always returns false in such cases.



