Handle Null and Non-Implementing Objects
In this step, we will examine how the instanceof
keyword behaves when dealing with null
references and objects that do not implement the interface being checked. Understanding these cases is important for writing robust code.
Let's modify our InterfaceCheck.java
file to include a null
reference and an object of a class that does not implement either Printable
or Editable
.
-
Open the InterfaceCheck.java
file in the ~/project
directory.
-
Replace the existing code with the following:
package project;
// Assume Printable and Editable interfaces and Document class are already defined
public class InterfaceCheck {
public static void main(String[] args) {
Object obj1 = new Document("Initial Content");
Object obj2 = "Just a String"; // Does not implement Printable or Editable
Object obj3 = null; // A null reference
Object obj4 = new Object(); // An object that does not implement Printable or Editable
System.out.println("Checking obj1 (Document):");
if (obj1 instanceof Printable) {
System.out.println("obj1 implements Printable");
} else {
System.out.println("obj1 does not implement Printable");
}
if (obj1 instanceof Editable) {
System.out.println("obj1 implements Editable");
} else {
System.out.println("obj1 does not implement Editable");
}
System.out.println("---");
System.out.println("Checking obj2 (String):");
if (obj2 instanceof Printable) {
System.out.println("obj2 implements Printable");
} else {
System.out.println("obj2 does not implement Printable");
}
if (obj2 instanceof Editable) {
System.out.println("obj2 implements Editable");
} else {
System.out.println("obj2 does not implement Editable");
}
System.out.println("---");
System.out.println("Checking obj3 (null):");
if (obj3 instanceof Printable) {
System.out.println("obj3 implements Printable");
} else {
System.out.println("obj3 does not implement Printable");
}
if (obj3 instanceof Editable) {
System.out.println("obj3 implements Editable");
} else {
System.out.println("obj3 does not implement Editable");
}
System.out.println("---");
System.out.println("Checking obj4 (Object):");
if (obj4 instanceof Printable) {
System.out.println("obj4 implements Printable");
} else {
System.out.println("obj4 does not implement Printable");
}
if (obj4 instanceof Editable) {
System.out.println("obj4 implements Editable");
} else {
System.out.println("obj4 does not implement Editable");
}
}
}
In this updated code, we've added two new Object
variables:
obj3
is assigned null
.
obj4
is assigned a new instance of the base Object
class, which does not implement our custom interfaces.
We then use instanceof
to check each of these objects against both Printable
and Editable
.
-
Save the file.
Now, let's compile and run the updated code.
-
Open the Terminal in the ~/project
directory.
-
Compile the Java files. Since we only modified InterfaceCheck.java
, we can compile just that file, but compiling all three is also fine:
javac Printable.java Editable.java Document.java InterfaceCheck.java
-
Run the InterfaceCheck
program:
java InterfaceCheck
You should see output similar to this:
Checking obj1 (Document):
obj1 implements Printable
obj1 implements Editable
---
Checking obj2 (String):
obj2 does not implement Printable
obj2 does not implement Editable
---
Checking obj3 (null):
obj3 does not implement Printable
obj3 does not implement Editable
---
Checking obj4 (Object):
obj4 does not implement Printable
obj4 does not implement Editable
Observe the output for obj3
(the null
reference). The instanceof
operator returns false
when the object reference is null
, regardless of the type being checked. This is a key behavior of instanceof
and prevents NullPointerException
errors when performing the check.
Also, observe the output for obj4
(the plain Object
). As expected, since the Object
class does not implement Printable
or Editable
, the instanceof
checks return false
.
This step demonstrates that instanceof
is safe to use with null
references and correctly identifies objects that do not implement the specified interface.