The instanceof operator of Java is used to determine whether an object(pointed by a reference variable) matches with a specified
type or not. This type could be a concrete class-type, an abstract class-type or an interface-type.
Syntax of an instanceof operator -
object-reference instanceof type;
Rules of instanceof operator -
Looking at the syntax of instanceof operator:
The instanceof operator returns true if an object-reference on its left,
points to an object of a matching type specified on its right, otherwise,
The instanceof operator returns false,
if there is a mismatch between the specified object reference and type.
A simple code with instanceof operator
//Java - Example of instanceof operator
class B
{
}
class A
{
public static void main(String... ar)
{
//Creating an object of class AA
A ob= new A();
//checking if ob refers to an object of class A
System.out.println(ob instanceof A);
//checking if ob refers to an object of class B
System.out.println(ob instanceof B);
}
}
Output
true
false
Program Analysis
We have created an object of class A, which is referenced by a reference variable, ob:
On checking if ob is pointing to an object of class A, instanceof operator returns true.
On checking if ob is pointing to an object of class B, instanceof operator returns false.
Important points to remember:
When a class extends another class, an object of a subclass can not only be assigned to an object-reference of its own class,
but can also be assigned to
an object-reference type of its superclass, because an object-reference of a superclass is big enough to refer to an object of its subclass.
When a class implements an interface, an object of such class can not only be assigned to an object reference of its own class,
but can also be assigned to an object-reference type of the implemented interface.
Using instanceof operator, when a class extends a concrete class.
When a class extends a concrete class, the instanceof operator returns true when a subclass object is checked against its concrete superclass-type.
//Java - Using instanceof operator when a class extends a concrete class
//Superclass
class B
{
}
//Subclass
class A extends B
{
public static void main(String... ar)
{
//An object of class A is referenced by a reference of its own type, A.
A ob1 = new A();
//An object of class A is referenced by its superclass reference type, B.
B ob2 = new A();
//An object of class B is referenced by a reference of its own type, B.
B ob3 = new B();
System.out.println(ob1 instanceof A);
System.out.println(ob1 instanceof B);
System.out.println(ob2 instanceof B);
System.out.println(ob2 instanceof A);
System.out.println(ob3 instanceof A);
}
}
Output
true
true
true
true
false
Program Analysis
Class A has extended class B:
The first four instanceof checks return true because we have checked an
object of subclass A against its own class-type or its superclass-type.
The last instanceof check returns false because the reference variable ob3 is of type B and it points to an object of type B,
therefore, ob3 mismatches with subclass-type A.
Advertisement
Using instanceof operator, when a class implements an interface.
When a class implements an interface, the instanceof operator returns true when an object of the implementer class
is checked against the implemented interface-type.
//Java - Using instanceof operator when a class implements an interface
interface Tree
{
public int age();
public boolean isAFruitTree();
}
class Mango implements Tree
{
public int age()
{
return 300;
}
public boolean isAFruitTree()
{
return true;
}
public static void main(String... ar)
{
Mango ob = new Mango();
System.out.println(ob instanceof Mango);
System.out.println(ob instanceof Tree);
}
}
Output
true
true
Program Analysis
In the last code, an interface Tree is implemented by a class Mango and hence, the instanceof operator returns true
not only when an object-reference of Mango class is checking against its own class-type, but also, when it is
checked against an interface-type that its class Mango has already implemented.
Using instanceof operator, when a class extends an abstract class.
When a class extends an abstract class,
the instanceof operator returns true when a subclass object is checked against its superclass, abstract class-type.
//Java - Using instanceof operator, when a class extends an abstract class
interface Cricket
{
public boolean isCricketTeamSport();
public boolean isCricketIndividialSport();
}
abstract class Football
{
public boolean isFootballTeamSport()
{
return true;
}
}
class Sports extends Football implements Cricket
{
public boolean isCricketTeamSport()
{
return true;
}
public boolean isCricketIndividialSport()
{
return false;
}
public static void main(String... ar)
{
Sports ob = new Sports();
System.out.println(ob instanceof Sports);
System.out.println(ob instanceof Football);
System.out.println(ob instanceof Cricket);
}
}
Output
true
true
true
Program Analysis
In the last code, an interface Cricket is implemented by a class, Sports and an abstract class Football is extended by the class, Sports. The instanceof operator
returns true -
When an object of class, Sports is checked against its own class-type.
When an object of class, Sports is checked against the interface-type Cricket, which Sports has implemented.
When an object of class, Sports is checked against the abstract class-type Football, which Sports has implemented.