Instanceof Keyword in Java - GeeksforGeeks

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

4/22/24, 8:34 PM instanceof Keyword in Java - GeeksforGeeks

instanceof Keyword in Java


Last Updated : 06 Jul, 2023
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

Examples of Java instaceof keyword


https://www.geeksforgeeks.org/instanceof-keyword-in-java/ 1/11
4/22/24, 8:34 PM instanceof Keyword in Java - GeeksforGeeks

Here are all the examples of instanceof keywords with their respective
cases:

Java1. Here Java


Arrays we Strings
will beJava
creating
OOPs sample classes
Java Collection Javawith a parent-child
8 Tutorial Java Multithreading Java Exceptio
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

https://www.geeksforgeeks.org/instanceof-keyword-in-java/ 2/11
4/22/24, 8:34 PM instanceof Keyword in Java - GeeksforGeeks

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

https://www.geeksforgeeks.org/instanceof-keyword-in-java/ 3/11
4/22/24, 8:34 PM instanceof Keyword in Java - GeeksforGeeks

// 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

https://www.geeksforgeeks.org/instanceof-keyword-in-java/ 4/11
4/22/24, 8:34 PM instanceof Keyword in Java - GeeksforGeeks

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

https://www.geeksforgeeks.org/instanceof-keyword-in-java/ 5/11
4/22/24, 8:34 PM instanceof Keyword in Java - GeeksforGeeks

Application of Java instanceof keyword


We have seen here that a parent class data member is accessed when a
reference of parent type refers to a child object. We can access child data
members using typecasting.
Syntax

((child_class_name) Parent_Reference_variable).func.name()

When we do typecasting, it is always a good idea to check if the typecasting


is valid or not. instanceof helps us here. We can always first check for
validity using instanceof, then do typecasting.

Example

Java

// Java program to demonstrate that non-method


// members are accessed according to reference
// type (Unlike methods which are accessed according
// to the referred object)

class Parent {
int value = 1000;
}

class Child extends Parent {


int value = 10;
}

// Driver class
class Test {
public static void main(String[] args)
{
Parent cobj = new Child();
Parent par = cobj;

// Using instanceof to make sure that par


// is a valid reference before typecasting
if (par instanceof Child) {
System.out.println(
"Value accessed through "
+ "parent reference with typecasting is "
+ ((Child)par).value);
}
}
}

https://www.geeksforgeeks.org/instanceof-keyword-in-java/ 6/11
4/22/24, 8:34 PM instanceof Keyword in Java - GeeksforGeeks

Output

Value accessed through parent reference with typecasting is 10

Feeling lost in the vast world of Backend Development? It's time for a
change! Join our Java Backend Development - Live Course and embark on an
exciting journey to master backend development efficiently and on schedule.
What We Offer:

Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks

Suggest improvement

Previous Next

final, finally and finalize in Java Stream In Java

Share your thoughts in the comments Add Your Comment

Similar Reads
Using throw, catch and instanceof to Pattern Matching For instanceof Java
handle Exceptions in Java 17

Record Pattern with instanceof in Java instanceof operator vs isInstance()


19 Method in Java

Comparison of static keyword in C++ Native Keyword in Java


and Java

https://www.geeksforgeeks.org/instanceof-keyword-in-java/ 7/11

You might also like