Java InstanceOf

    Java InstanceOf

    The Java InstanceOf is an operator that allows you to check if the specified object is an instance of the given class or interface.

    You can use this operator as a comparison operator to compare the instance with type and return true or false. If this operator is applied with the variable having the null value then it will return false.

    Example -

    class Simple{  
    
    int var=100;
    
    
    
      public static void main(String args[]){  
    
        Simple b = new Simple();  
    
        System.out.println(b instanceof Simple);  
    
      }  
    
    }  

    Output-

    True

    Example with Inheritance-

    class demo{
    
    int var=10;}
    
    
    
    
    class Simple extends demo{  
    
    int var=100;
    
    
    
      public static void main(String args[]){  
    
        Simple b = new Simple();  
    
        System.out.println(b instanceof demo);
    
      }  
    
    }  

    Output-

    True

    Example with Null value-

    //class demo{
    
    //int var=10;}
    
    
    
    
    class Simple{  
    
    int var=100;
    
    
    
      public static void main(String args[]){  
    
        Simple b = null;  
    
        System.out.println(b instanceof Simple);
    
      }  
    
    }  

    Output-

    False

    Downcasting in Java

    Downcasting is only possible if we use the instanceof operator. It is the process when we use the type of subclass to refer to the parent class’s object. You cannot use typecasting or can implement the reference directly you will get an error.

    Example-

    class demo{
    
    int var=10;}
    
    
    
    class Simple extends demo{  
    
    int var=100;
    
    
    
      public static void main(String args[]){  
    
        Simple b = new demo();  
    
        System.out.println(b instanceof demo);
    
      }  
    
    }  

    In the above example, we will get a compilation error.

    Example-

    class demo{
    
    int var=10;}
    
    
    
    class Simple extends demo{  
    
    int var=100;
    
    
    
      public static void main(String args[]){  
    
        Simple b = (Simple)new demo();  
    
        System.out.println(b instanceof demo);
    
      }  
    
    } 

    The above example will get compiled successfully but will throw a runtime exception.

    Example with downcasting -

    class demo{
    
    int var=10;}
    
    class Down extends demo{  
    
    static void display(demo d){
    
    if (d instanceof demo){
    
    Down s= (Down)d;
    
     System.out.println("downcasting");
    
    }
    
    }}
    
    
    
    class Simple{
    
      public static void main(String args[]){  
    
        demo d= new Down();  
    
        Down.display(d);
    
      }  
    
    } 

    Output-

    downcasting

    In the above example, the instanceof operator provides downcasting.

    Implementing downcasting without instanceof operator-

    You can also implement downcasting without the use of the instanceof operator.

    Example-

    class demo{
    
    int var=10;}
    
    
    
    class Down extends demo{  
    
    static void display(demo d){
    
    //if (d instanceof demo){
    
    Down s= (Down)d;
    
     System.out.println("downcasting");
    
    }
    
    }
    
    
    
    class Simple{
    
      public static void main(String args[]){  
    
        demo d= new Down();  
    
        Down.display(d);
    
      }  
    
    } 

    Output-

    downcasting