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:
- 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
- class Main {
- public static void main (String [] args) {
- String name = “Programming”;
- Integer age = 22;
-
- System.out.println (“Is name an instance of String:” + (name instanceof String));
- System.out.println (“Is age an instance of Integer:” + (age instanceof Integer));
- }
- }
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 object 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
- class Animal {
- }
- class Dog extends Animal {
- }
- class Main {
- public static void main (String [] args) {
- Dog d1 = new Dog ();
- System.out.println (“Is d1 an instance of Dog:” + (d1 instanceof Dog));
- System.out.println (“Is d1 an instance of Animal:” + (d1 instanceof Animal));
- }
- }
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
- class Animal {
- }
- class Dog {
- }
- class Cat {
- }
- class Main {
- public static void main (String [] args) {
- Dog d1 = new Dog ();
- Animal a1 = new Animal ();
- Cat c1 = new Cat ();
-
- System.out.println (“Is d1 an instance of the Object class:“ + (d1 instanceof Object));
- System.out.println (“Is a1 an instance of the Object class:“ + (a1 instanceof Object));
- System.out.println (“Is c1 an instance of the Object class:“ + (c1 instanceof Object));
- }
- }
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 whether 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
- class Animal {
- public void displayInfo () {
- System.out.println (“I am an animal.”);
- }
- }
-
- class Dog extends Animal {
- }
-
- class Main {
- public static void main (String [] args) {
- Dog d1 = new Dog ();
- Animal a1 = d1;
- a1.displayInfo ();
- }
- }
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
- class Animal {
- }
-
- class Dog extends Animal {
- public void displayInfo () {
- System.out.println (“I am a dog.”);
- }
- }
-
- class Main {
- public static void main (String [] args) {
- Dog d1 = new Dog ();
- Animal a1 = d1; // Upcasting
- Dog d2 = (Dog) a1; // Downcasting
- d2.displayInfo ();
- }
- }
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
- class Animal {
- }
-
- class Dog extends Animal {
- public void displayInfo () {
- System.out.println (“I am a dog”);
- }
- }
-
- class Main {
- public static void main (String [] args) {
- Dog d1 = new Dog ();
- Animal a1 = d1; // Upcasting
-
- if (a1 instanceof Dog) {
- Dog d2 = (Dog) a1; // Downcasting
- d2.displayInfo ();
- }
- }
- }
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
- Animal interface {
- }
- class Dog implements Animal {
- }
-
- class Main {
- public static void main (String [] args) {
- Dog d1 = new Dog ();
- System.out.println (“Is d1 an instance of Animal:” + (d1 instanceof Animal));
- }
- }
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.