Examples of Java instaceof keyword
Here are all the examples of instanceof keywords with their respective cases:
1. Here we will be creating sample classes with a parent-child relationship.
Java
// Java program to demonstrate working of instanceof Keyword // Class 1 // Parent class class Parent { } // Class 2 // Child class class Child extends Parent { } // Class 3 // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating object of child class Child cobj = new Child(); // A simple case if (cobj instanceof Child) System.out.println( "cobj is instance of Child" ); else System.out.println( "cobj is NOT instance of Child" ); // instanceof returning true for Parent class also if (cobj instanceof Parent) System.out.println( "cobj is instance of Parent" ); else System.out.println( "cobj is NOT instance of Parent" ); // instanceof returns true for all ancestors // Note : Object is ancestor of all classes in Java if (cobj instanceof Object) System.out.println( "cobj is instance of Object" ); else System.out.println( "cobj is NOT instance of Object" ); } } |
Output
cobj is instance of Child cobj is instance of Parent cobj is instance of Object
2. instanceof returning false for null
Java
// Java program to demonstrate that instanceof // returns false for null class Test { } class Main { public static void main(String[] args) { Test tobj = null ; // A simple case if (tobj instanceof Test) System.out.println( "tobj is instance of Test" ); else System.out.println( "tobj is NOT instance of Test" ); } } |
Output
tobj is NOT instance of Test
3. Parent object is not an instance of Child
Java
// A Java program to show that a parent object is // not an instance of Child class Parent { } class Child extends Parent { } // Driver Class class Test { // main function public static void main(String[] args) { Parent pobj = new Parent(); if (pobj instanceof Child) System.out.println( "pobj is instance of Child" ); else System.out.println( "pobj is NOT instance of Child" ); } } |
Output
pobj is NOT instance of Child
4. Parent reference referring to a Child is an instance of a Child
Java
// A Java program to show that a parent reference // referring to a Child is an instance of Child class Parent { } class Child extends Parent { } // Driver class class Test { // main function public static void main(String[] args) { // Reference is Parent type but object is // of child type. Parent cobj = new Child(); if (cobj instanceof Child) System.out.println( "cobj is instance of Child" ); else System.out.println( "cobj is NOT instance of Child" ); } } |
Output
cobj is instance of Child
instanceof Keyword in Java
In Java, instanceof is a keyword used for checking if a reference variable contains a given type of object reference or not. Following is a Java program to show different behaviors of instanceof. Henceforth 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.
Example of the Java instanceof Keyword
Java
// Java Program to Illustrate instanceof Keyword // Importing required I/O classes import java.io.*; // Main class class GFG { public static void main(String[] args) { // Creating object of class inside main() GFG object = new GFG(); // Returning instanceof System.out.println(object instanceof GFG); } } |
Output
true
Contact Us