InstanceOf in Java is used for determining the relationship of an object with its class when the inheritance concept is implemented in the Java code snippet. The instanceOf class, in general, is applicable for the object-oriented programming languages that allow the inheritance, and it returns the output values in the form of boolean results, that is, either true or false. If the instanceOf class variable has a NULL value, then the class returns the output as ‘false’. As this class is used for comparison purposes, it is also called as ‘type comparison operator’.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
The instanceOf class is used to check if the object is of any class or not.
obj instanceOf object
Above is the standard syntax for instanceOf class. Here, the obj is the name of the object that must have been created earlier (reference). instanceOf is the keyword, and the object is the class or the interface with which we match the obj object.
In various cases, instanceOf can be proved to be of major use, like where we have a collection of objects, and you are not sure of which object it refers to. An example of such a case can be a simple form with many controls.
Also, if in case we use instanceOf with a variable that has a NULL value, it is sure to return false.
The instanceOf operator in java works on a simple is-a relationship. Simply stating, is-a relationship is an object-oriented concept, where we compare or, say, work on a relation among abstractions, where class A is a subclass of class B. This is a relationship totally based on inheritance. In other words, it is like saying, “X is of Y type”.
Now, let’s understand the working of instanceOf, along with the respective code.
First, we’ll create a class named Parent.
Code:
class Parent{ } //Then let’s add a simple main class. public static void main(String args[]) { }
We’ll then create an instance of our Parent class.
Parent child = new Parent();
Finally, we’ll use the instanceOf operator to check the relationship between child and Parent. Which goes like this: child instanceOf Parent
Now, as mentioned earlier, the syntax for instanceOf goes from an object that has to check, then the instanceOf keyword and then the class/interface with which the given object is to be tested.
At any point where we encounter with the keyword “extends” or “implements” in a class declaration, it’s a clear sign of is-a relationship being used.
The following example demonstrates a single line use of instanceOf.
class instanceof_java{ public static void main(String args[]) { instanceof_java s = new instanceof_java(); System.out.println(s instanceOf instanceof_java); } }
Code Interpretation: Started with creating a simple class instanceof_java. In the class instanceof_java, we have our main class, and within our main class, we have an object s created. This object s is of instanceof_java type. Then to implement the working of instanceOf, we provided an output statement with object s. In the last line, we passed s along with the instanceof keyword and the parent class. Upon execution, the code will return ‘true’ because the object s is of instanceof type.
Moving further, if we have an object of our known class or interface but have not assigned any value to the same object, it is bound to return false, even though we are of the same class.
class instanceof_sample{ public static void main(String args[]) { instanceof_sample new = null; System.out.println(new instanceOf instanceof_sample); } }
Here we have a similar code as we had for the earlier example, but with a null value object. When executed, this code will return false. As we can see, object new is the instance of instanceof_sample, but new is being assigned with a null value, which returns in false.
Based on whether the object ref is not null and an instance of the referred type. When X is the simple class of the object referred, and Y is the resolved class or an array of an interface type.
Finally, we will demonstrate an instanceOf program to understand that the parent object cannot be an instance of the child class.
Program
class Subject { } class Topic extends Subject { } class instanceof_java { public static void main(String[] args) { Subject history = new Subject (); if (history instanceof Topic) System.out.println("history is instance of Topic"); else System.out.println("history is NOT instance of Topic"); } }
Code Interpretation: For the purpose of understanding the instanceOf operator in different scenarios, we wrote the above code. We created a simple class Subject and then another class Topic, which extends class Subject, making the class Topic as child and class Subject as Parent here. Then another class with the main method. Within the main method, we created a new instance of parent class Subject. In the IF ELSE loop, we checked the instance object’s condition with the parent class Subject. If the condition was fulfilled, it would return “history is an instance of Topic” and “history is NOT an instance of Topic” if the condition fails.
Upon executing the above code, the output will be “history is NOT an instance of Topic”, meaning the condition passed in IF fails. It happened because we tried to check the parent of the object “history” with the class Topic. We know that the class Topic extends the class Subject, meaning Topic is a subclass to Subject. And when we tried to check the type of history with class Topic, it returns false (NOT). This means the Parent Object cannot be an instance of a class.
Output:
We have learned about instanceOf class in Java, which simply decides if the object is of the given type. We understood how is-a relationship impacts on instanceOf operator. Also known as a comparison operator, instanceOf is based on inheritance.
The above is the detailed content of instanceOf in Java. For more information, please follow other related articles on the PHP Chinese website!