< Prev
Next >



Java instanceof Operator





The instanceof operator in Java is used to know whether an object(pointed by a reference variable) is of a particular type or not.




Syntax of an instanceof operator -


object-reference  instanceof  class-type;




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 this code, interface Cricket is implemented by the class Sports and abstract class Football is extended by class Sports. The instanceof operator returns true -



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< Conditional Operator
Character Wrapper Class>

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



Please check our latest addition

PYTHON