Java instanceof

In Java, instanceof is a keyword used for checking if a reference variable contains a given type of object reference or not(class or subclass or interface).It is known as a comparison operator where the instance is getting compared to type returning boolean true or false as in Java we do not have 0 and 1 boolean return types.If we apply the instanceof operator with any variable that has null value, it returns false.

Java instanceof Examples :

Programs 1 :

class School{  
 public static void main(String args[]){  
 School s=new School();  
 System.out.println(s instanceof School);//true  
 }  
}  

Output:true

Program 2 :

class Animal{}  
class Dog extends Animal{//Dog inherits Animal  
  
 public static void main(String args[]){  
 Dog d=new Dog();  
 System.out.println(d instanceof Animal);//true  
 }  
}  

Output:true

Programs 3 : Java instanceof in java with a null value variable

class Dog{  
 public static void main(String args[]){  
  Dog d=null;  
  System.out.println(d instanceof Dog);//false  
 }  
}  

Output:false

Downcasting with java instanceof operator

When any Subclass refers to the Parent class object, it is known as downcasting. If we perform it directly, compiler gives Compilation error. If we use typecasting, ClassCastException is thrown at runtime. But if we use instanceof operator, downcasting is possible.

Dog d=new Animal();//Compilation error  

If we perform downcasting by typecasting, ClassCastException is thrown at runtime.

Dog d=(Dog)new Animal();  //Compiles successfully but ClassCastException is thrown at runtime  

Prgram 4 : Possibility of downcasting with instanceof

class Animal { }  
class Dog extends Animal {  
  static void method(Animal a) {  
    if(a instanceof Dog){  
       Dog d=(Dog)a;//downcasting  
       System.out.println("Downcasting is performing");  
    }  
  }  
   
  public static void main (String [] args) {  
    Animal a=new Dog();  
    Dog.method(a);  
  }  
    
 }  
 
Output:Downcasting is performing

Program 5 : Downcasting without the use of java instanceof

class Animal { }  
class Dog extends Animal {  
  static void method(Animal a) {  
       Dog d=(Dog)a;//downcasting  
       System.out.println("Downcasting is performing");  
  }  
   public static void main (String [] args) {  
    Animal a=new Dog();  
    Dog.method(a);  
  }  
}  

Output:Downcasting is performing

Program 6 : Downcasting can also be performed without the use of instanceof operator

class Animal { }  
class Dog extends Animal {  
  static void method(Animal a) {  
       Dog d=(Dog)a;//downcasting  
       System.out.println("Downcasting is performing");  
  }  
   public static void main (String [] args) {  
    Animal a=new Dog();  
    Dog.method(a);  
  }  
}  

Output:Downcasting is performing

Program 7 : Real-time use of instanceof in java

interface Printit {}  
class HR implements Printit{  
public void hr_office(){System.out.println("hr office");}  
}  
class CodingTeam implements Printit{  
public void coding_office(){System.out.println("coding office");}  
}  
class Company{  
void invoke(Printit p){//upcasting  
if(p instanceof HR){  
HR hr_off=(HR)p;//Downcasting   
hr_off.hr_office();  
}  
if(p instanceof CodingTeam){  
CodingTeam coding_off=(CodingTeam)p;//Downcasting   
coding_off.coding_office();  
}  
  
}  
}//end of Call class  
class Result{  
public static void main(String args[]){  
Printit p=new CodingTeam();  
Company c=new Company();  
c.invoke(p);  
}  
}  

Output: coding office
Scroll to Top