Instanceof operator training in Java (in quite simple language)

In Java, the instanceof keyword is a binary operator. Used to check if an object is an instance of a particular class.

This operator also checks whether an object is an instance of a class that implements an interface.

The structure of the instanceof operator is as follows:

  1. result = objectName instanceof className;

The left instanceof statement is the object name and the right statement is the class name. If the object is an instance of the class, the result is correct, otherwise it is false.

Example 1: instanceof in Java

  1. class Main {
  2. public static void main (String [] args) {
  3. String name = “Programming”;
  4. Integer age = 22;
  5. System.out.println (“Is name an instance of String:” + (name instanceof String));
  6. System.out.println (“Is age an instance of Integer:” + (age instanceof Integer));
  7. }
  8. }

Output

Is name an instance of String: true

Is age an instance of Integer: true

In the example above, we created a name object of type String and an age object of type Integer. Then we used the instanceof operator to check if the name is String and the age is Integer.

Use instanceof in inheritance

In inheritance, the instanceof operator is used to check whether an object of subclass is an instance of superclass.

Example 2: instanceof in inheritance

  1. class Animal {
  2. }
  3. class Dog extends Animal {
  4. }
  5. class Main {
  6. public static void main (String [] args) {
  7. Dog d1 = new Dog ();
  8. System.out.println (“Is d1 an instance of Dog:“ + (d1 instanceof Dog));
  9. System.out.println (“Is d1 an instance of Animal:“ + (d1 instanceof Animal));
  10. }
  11. }

Output

Is d1 is an instance of Dog: true

Is d1 an instance of Animal: true

In the example above, d1 is an example of a Dog and Animal class. Hence, both d1 instanceof Dog and d1 instanceof Animal are true.

Object class

In Java, all classes inherit from the Object class. The extends keyword is not used in inheriting the Object class. This is an exception in Java.

Example 3: Object class

  1. class Animal {
  2. }
  3. class Dog {
  4. }
  5. class Cat {
  6. }
  7. class Main {
  8. public static void main (String [] args) {
  9. Dog d1 = new Dog ();
  10. Animal a1 = new Animal ();
  11. Cat c1 = new Cat ();
  12. System.out.println (“Is d1 an instance of the Object class:“ + (d1 instanceof Object));
  13. System.out.println (“Is a1 an instance of the Object class:“ + (a1 instanceof Object));
  14. System.out.println (“Is c1 an instance of the Object class:“ + (c1 instanceof Object));
  15. }
  16. }

Output

Is d1 an instance of the Object class: true

Is a1 an instance of the Object class: true

Is c1 an instance of the Object class: true

In the example above, we created objects a1, d1, and c1 from the Animal, Dog, and Cat classes, respectively. We used the instanceof operator to check if these objects a1, d1 and c1 are also objects of the Object class. The output results are true for everyone.

Because the Object class is the root class defined in the java.lang package. All other classes are child classes of the Object class that form the hierarchy in Java.

Upcasting and downcasting objects

In Java, an object of subclass may behave like an object of superclass. This is called upcasting.

The Java compiler does the upcasting automatically.

Example 4: Upcasting object

  1. class Animal {
  2. public void displayInfo () {
  3. System.out.println (“I am an animal.”);
  4. }
  5. }
  6. class Dog extends Animal {
  7. }
  8. class Main {
  9. public static void main (String [] args) {
  10. Dog d1 = new Dog ();
  11. Animal a1 = d1;
  12. a1.displayInfo ();
  13. }
  14. }

Output

I am an animal.

In the example above, we have created a d1 object of class Dog. We use that d1 object to create an a1 object of class Animal with upcasting.

The code runs without any problems. Because upcasting is done using Java compilers.

Downcasting is a reverse upcasting method.

In the case of upcasting, an object of the superclass behaves like an object of the subclass. We must explicitly instruct the compiler to downcast in Java.

Example 5: Object downcasting problem

  1. class Animal {
  2. }
  3. class Dog extends Animal {
  4. public void displayInfo () {
  5. System.out.println (“I am a dog.”);
  6. }
  7. }
  8. class Main {
  9. public static void main (String [] args) {
  10. Dog d1 = new Dog ();
  11. Animal a1 = d1; // Upcasting
  12. Dog d2 = (Dog) a1; // Downcasting
  13. d2.displayInfo ();
  14. }
  15. }

When we run the program, we encounter the exception of ClassCastException. Let’s see what happens.

Here, we have created an a1 object from the Animal superclass. Then we set object a1 equal to object d1 of Dog.

This created a problem because an a1 object from the Animal superclass might also refer to other subclasses. If we had created another Cat subclass with Dog, Animal might be Cat or maybe Dog and that would be confusing.

To solve this problem we can use the instanceof operator.

Example 6: Solve downcasting using instanceof

  1. class Animal {
  2. }
  3. class Dog extends Animal {
  4. public void displayInfo () {
  5. System.out.println (“I am a dog”);
  6. }
  7. }
  8. class Main {
  9. public static void main (String [] args) {
  10. Dog d1 = new Dog ();
  11. Animal a1 = d1; // Upcasting
  12. if (a1 instanceof Dog) {
  13. Dog d2 = (Dog) a1; // Downcasting
  14. d2.displayInfo ();
  15. }
  16. }
  17. }

Output

I am a dog

In the example above, we use the instanceof operator to check if a1 is an instance of the Dog class. Downcasting is done only when the expression Dog a1 instanceof is correct.

instanceof in the interface

Use the instanceof operator to check if an object in the class is also an instance of the interface in which the implemented class is used.

Example 7: instanceof in the interface

  1. Animal interface {
  2. }
  3. class Dog implements Animal {
  4. }
  5. class Main {
  6. public static void main (String [] args) {
  7. Dog d1 = new Dog ();
  8. System.out.println (“Is d1 an instance of Animal:“ + (d1 instanceof Animal));
  9. }
  10. }

Output

Is d1 an instance of Animal: true

In the example above, we created a Dog class that implements the Animal interface.

Object d1 is then created from the Dog class. We used the instanceof operator to check if d1 is also an instance of the Animal interface.

Leave a Reply

Your email address will not be published. Required fields are marked *