Instanceof Keyword in Java - GeeksforGeeks
Instanceof Keyword in Java - GeeksforGeeks
Instanceof Keyword in Java - GeeksforGeeks
Java
// Main class
class GFG {
public static void main(String[] args)
{
// Returning instanceof
System.out.println(object instanceof GFG);
}
}
Output
true
Here are all the examples of instanceof keywords with their respective
cases:
Java
// Class 1
// Parent class
class Parent {
}
// Class 2
// Child class
class Child extends Parent {
}
// Class 3
// Main class
class GFG {
// 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");
https://www.geeksforgeeks.org/instanceof-keyword-in-java/ 2/11
4/22/24, 8:34 PM instanceof Keyword in Java - GeeksforGeeks
Output
Java
https://www.geeksforgeeks.org/instanceof-keyword-in-java/ 3/11
4/22/24, 8:34 PM instanceof Keyword in Java - GeeksforGeeks
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
Java
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
Java
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
https://www.geeksforgeeks.org/instanceof-keyword-in-java/ 5/11
4/22/24, 8:34 PM instanceof Keyword in Java - GeeksforGeeks
((child_class_name) Parent_Reference_variable).func.name()
Example
Java
class Parent {
int value = 1000;
}
// Driver class
class Test {
public static void main(String[] args)
{
Parent cobj = new Child();
Parent par = cobj;
https://www.geeksforgeeks.org/instanceof-keyword-in-java/ 6/11
4/22/24, 8:34 PM instanceof Keyword in Java - GeeksforGeeks
Output
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
Similar Reads
Using throw, catch and instanceof to Pattern Matching For instanceof Java
handle Exceptions in Java 17
https://www.geeksforgeeks.org/instanceof-keyword-in-java/ 7/11