Google+

210. instanceof operator








instanceof is a binary operator used to verify whether the given object is an instance or sub-type of the given Class. instanceof operator returns true if the given object is an instance or sub-type of the given Class.

Practice all the below given examples to understand the usage and capability of instanceof operator.

Example 1 -

//Object of a Class is an instance of its Class.

ClassOne object1 = new ClassOne( );

if(object1 instanceof ClassOne)
  System.out.println("object1 is an instance of ClassOne");
else
  System.out.println("object1 is not an instance of ClassOne");

Output -

object1 is an instance of ClassOne.

Lets implement the Example 1 on Eclipse IDE -

1. Create a new Java Project 'Project 38' as shown below -


2. Create a new Java Class file 'ClassA.java' with main( ) method as shown below -


3. Create a Class 'ClassOne' in the same 'ClassA.java' file as shown below -


4. Create an object for 'ClassOne' class in 'ClassA' class as shown below -


5. Use instanceof operator to findout whether the object 'object1' is an instance of 'ClassOne' Class as shown below and use the print statements to print the result to the output -


6. Run the Java Class 'ClassA.java' and observe that the output 'Object1 is an instance of ClassOne Class" is displayed as shown below -



Example 2 -

//Object of Sub Class is an instance of Super Class
//Class Dog is a subclass of Class Animal.

Dog dog = new Dog( );

System.out.println("dog object is an instance of Animal Class is "+ (dog instanceof Animal) );

Output -

dog object is an instance of Animal Class is true

Lets implement the Example 2 on Eclipse IDE -

1. Create a new Java Class 'ClassB.java' with main( ) method in the same Java Project 'Project 38' as shown below -


2. Create two Classes 'Animal' and 'Dog' in the same 'ClassB.java' Class file as shown below -


3. Make Dog Class as a SubClass of Animal Class as shown below -


4. Create an Object for the Sub Class Dog Class as shown below -


5. Now find out whether the sub class object 'dog' is an instance of Super Class 'Animal' using instanceof operator as shown below -


6. Run the Java Class file 'ClassB.java' and verify the result in Output Console as shown below -


Example 3 -

//Object of a Class is an instance of an implemented Class
//Class Pig implements Mammal Interface

Pig pig= new Pig( );

System.out.println("pig object is an instance of Mammal Interface is "+ (pig instanceof Mammal) );

Output -

pig object is an instance of Mammal Interface is true

Lets implement the Example 3 on Eclipse IDE -

1. Create a Java Class file 'ClassC.java' with main( ) method in the same Java Project 'Project 38' as shown below -


2. Create 'Mammal.java' Interface file in the same Project 'Project 38' as shown below -



3. Now go back to 'ClassC.java' Class file and create 'Pig' Class as shown below -



4. Make the Pig Class implement the Interface Mammal as shown below -


5. Create an Object for Pig Class in 'ClassC' Class as shown below -


6. Now use instanceof Operator to find out whether the object of Pig Class is an instance of its implemented Interface Mammal as shown below -


7. Run the Java Class file 'ClassC.java' and observe that the result is displayed in the Console tab as output as shown below -



Example 4 -

Frog frog = null;  //frog object referring to null i.e. no object

System.out.println("frong object is an instance of Frog Class is "+ (frog instanceof Frog) );

Output -

frog object is an instance of Frog Class is false

Lets implement the Example 4 on Eclipse IDE -

1. Create a Class file 'ClassD.java' with main( ) method  in the existing Project 'Project 38' as shown below -

2. Create a Class 'Frog' in the same 'ClassD.java' Class file as shown below -


3. Define an object for Frog Class and assign null reference to the defined object as shown below -


4. Now find out whether the frog object is an instance of Frog Class using the instanceof operator as shown below -


5. Save and Run the Java Class file 'ClassD.java' and observe that the result is displayed in the Console tab as output as shown below -


Hence the object assigned with null reference wont be an instance to its defined Class.

Example 5 -

//After Upcasting, objects of the Super Class and Sub Class are instances of both SuperClass and SubClass
//Use Object Class in this example as it is the Super Class of all the Classes in Java

Monkey monkey = new Monkey( );
Object obj = monkey;

System.out.println("obj object is an instance of Object Class is "+ (obj instanceof Object) );
System.out.println("obj object is an instance of Monkey Class is "+ (obj instanceof Monkey) );
System.out.println("monkey object is an instance of Object Class is "+ (monkey instanceof Object) );
System.out.println("monkey object is an instance of Monkey Class is "+ (monkey instanceof Monkey) );

Output -

obj object is an instance of Object Class is true
obj object is an instance of Monkey Class is true
monkey object is an instance of Object Class is true
monkey object is an instance of Monkey Class is true

Lets implement the Example 3 on Eclipse IDE -

1. Create a Java Class file 'ClassE.java' in the existing Java Project 'Project 38' as shown below -


2. Create a Class 'Monkey' in the same 'ClassE.java' file as shown below -


3. Create an object for Monkey Class in the 'ClassE' class as shown below -



4. Define an object for Object Class and assign the object of Monkey Class to it as shown below -



5. Now find out whether the objects of Monkey Class and its super Class Object are instances of Monkey and Object Classes by writing the following four statements as shown below -


6. Save and Run the Java Class 'ClassE.java' and observe that the results are displayed in the Console tab as output as shown below -


Hence after Upcasting the objects of Sub Class and Super Class are instances of both Sub and Super Classes.

Example 6 -

//Object of Super Class is not an instance of Sub Class
//Assuming Benz is Sub Class and Car is its Super Class

Car car = new Car( );

System.out.println("car object is an instance of Benz Class is "+(car instanceof Benz) );

Output -

car object is an instance of Benz Class is false

Lets implement this Example 6 on Eclipse IDE -

1. Create a Java Class file 'ClassF.java' in the Existing Java Project 'Project 38' as shown below -


2. Create two Classes 'Car' and 'Benz'  in the same 'ClassF.java' Class file as shown below -


3. Make Car Class as a super class for Benz Class as shown below -


4. Create an object for Car Class as shown below -


5. Now find out whether the object of Super Class Car is an instance of Sub Class Benz Class by writing the code as shown below -


6. Save and Run the Java Class file 'ClassF.java' and view the result in the Console tab as output as shown below -


Hence Sub Class cant be an instance for a Super Class object.

Example 7 -

As you already know we can Upcast an object of Sub Class to its just defined Super Class object. After Upcasting, we know that the object of the Super Class will be the instance of Super Class and Sub Class.
We use instanceof operator with the object of Super Class to find whether it is an instance of the Sub Class and downgrade the object of the Super Class to the Sub Class. Object of the Super Class can be downgraded safely without any problems, when the object of its sub class is upgraded and assigned to the just defined Super Class object as shown below -
Cat c1 = new Cat(); 
Animal a = c1; //upcasting to Animal
if(a instanceof Cat){ // testing if the Animal is a Cat
System.out.println("It's a Cat! Now i can safely downcast it to a Cat, without a fear of failure.");
Cat c2 = (Cat)a;
}
The above program when executed , will pass the statement in the if condition and downcast the Super Class object 'a' to Sub Class Cat Class type.

So we have used instanceof operator in the above example to find out whether the object stored in the Animal defined object is a cat. If the object stored in the Animal object a is a cat, then we can easily downcast the cat to a cat without any problems.

Lets assign a dog to the Super Class defined object 'a' as shown below -

Dog d1 = new Dog(); 
Cat c1 = new Cat(); 
Animal a = d1; //upcasting to Animal
if(a instanceof Cat){ // testing if the Animal is a Cat
System.out.println("It's a Cat! Now i can safely downcast it to a Cat, without a fear of failure.");
Cat c2 = (Cat)a;
}
 In the above example, as dog is assigned to the Animal defined object, the if condition fails and the conversion of dog inside Animal object to a cat wont be done.

Hence we can use instanceof operator to find out whether we can downcast an object to any type.






Please comment below to feedback or ask questions.

Creating Duplicate Classes in same Project  will be explained in the next post.









No comments: