Saturday, December 3, 2016

Java instanceof Operator with examples

In this section we will understand what is instanceof operator and how to use instanceof operator in our java programming language. In previous post, i had shared when to use abstract class and when interface in java, this is one of the tricky core java interview question,therefore i highly recommended read and understand the differences and where we use in real time scenario. Now let we discuss today topic about java instanceof keyword in java. In java, the instanceof operator is used to test the objects to see if they belongs to particular class type. That means if you want to determine a class if it belongs to a specific given class or for implementing a given interface,you can make use of instanceof operator.

Remember that the word instance means object. The main purpose of this operator is if you have an object,if that object particular type or not,so to check or test that object then we should go for instanceof operator. This operator is also called type comparison operator. The instanceof operator returns true or false depending on the results of the comparison. The instanceof operator returns true if the given the object belongs to certain class or if the given object implements interface. it will return false if it is not belong to certain class.

The general form of instanceof operator:

objref instanceof type 

In the above syntax, objref is a reference to an instance of a class and type is a class type. If objref is of specified type or can be cast into the specified type then it returns ture,otherwise false.

Let us understand this concept with the following example:

class Vehicle
{
String name;
Vehicle()
{
name="vehicle";
}
}
class HeroHonda extends Vehicle

{
HeroHonda()
{
name="HeroHonda";
}
}
class Bajaj extends Vehicle
{
Bajaj(){
name="Bajaj";
}
}
public class InstanceOfTest
{
static boolean result;
static HeroHonda hh=new HeroHonda();
static Bajaj bj=new Bajaj();
static HeroHonda hh1=null;
public static void main(String[] args)
{
result=hh instanceof HeroHonda;
System.out.print("hh is an HeroHonda:"+result+ "\n");
resutl=bj instanceof Bajaj;
System.out.print("bj is an bajaj:"+result+ "\n");
result=hh1 instanceof HeroHonda;
System.out.print("hh1 is an HeroHonda:"+result "\n");
}


Output:







I hope you understand well by using extend keyword we have done above program. Now, we will understand how to use implementes keyword using instanceof operator. 

Example:

interface Animal
{
}
class Tiger implements Animal
{
}

public class Cat implements Animal
{
public static void main(String[] args)
{
Tiger t=new Tiger();

Cat c=new Cat();
System.out.println(t instanceof Animal);
System.out.println(c instanceof Animal);
}
}


Output:





I hope you enjoy this post and give your comments below and share this page to your friends as well.
 

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...