Introduction
In this lab, you will learn how to check if an object implements a specific interface in Java. We will explore the use of the instanceof keyword, a fundamental tool for runtime type checking.
Through hands-on exercises, you will apply instanceof to verify interface implementation, test scenarios involving multiple interfaces, and handle cases with null or non-implementing objects. This lab will provide you with practical skills for determining the actual type of an object and leveraging interface-specific functionalities.
Apply instanceof for Interface Check
In this step, we will explore how to use the instanceof keyword in Java to check if an object is an instance of a specific interface. This is a common task when you have a variable of a superclass or interface type and you need to determine the actual type of the object it refers to, especially if you want to call methods specific to a particular interface.
First, let's define a simple interface and a class that implements it.
Open the WebIDE editor.
In the File Explorer on the left, make sure you are in the
~/projectdirectory.Create a new file named
Printable.java. You can do this by right-clicking in the File Explorer and selecting "New File", then typingPrintable.java.Open
Printable.javaand add the following code:package project; public interface Printable { void print(); }This defines a simple interface named
Printablewith one method,print().Save the file (Ctrl+S or Cmd+S).
Now, let's create a class that implements the Printable interface.
In the
~/projectdirectory, create a new file namedDocument.java.Open
Document.javaand add the following code:package project; public class Document implements Printable { private String content; public Document(String content) { this.content = content; } @Override public void print() { System.out.println("Printing Document: " + content); } }This class
Documentimplements thePrintableinterface and provides an implementation for theprint()method.Save the file.
Finally, let's create a main class to demonstrate the use of instanceof with the interface.
In the
~/projectdirectory, create a new file namedInterfaceCheck.java.Open
InterfaceCheck.javaand add the following code:package project; public class InterfaceCheck { public static void main(String[] args) { Object obj1 = new Document("Important Report"); Object obj2 = "Just a String"; // Check if obj1 is an instance of Printable if (obj1 instanceof Printable) { System.out.println("obj1 implements Printable"); Printable p1 = (Printable) obj1; // Cast to Printable p1.print(); // Call the print method } else { System.out.println("obj1 does not implement Printable"); } System.out.println("---"); // Check if obj2 is an instance of Printable if (obj2 instanceof Printable) { System.out.println("obj2 implements Printable"); Printable p2 = (Printable) obj2; // This line would cause a ClassCastException if executed p2.print(); } else { System.out.println("obj2 does not implement Printable"); } } }In this code:
- We create two
Objectvariables,obj1which refers to aDocumentobject (which implementsPrintable), andobj2which refers to aStringobject (which does not implementPrintable). - We use
if (obj1 instanceof Printable)to check if the object referenced byobj1is an instance of thePrintableinterface. - If it is, we print a message and then cast
obj1to thePrintabletype using(Printable) obj1. Casting allows us to treat theObjectas aPrintableand call itsprint()method. - We do the same check for
obj2. SinceStringdoes not implementPrintable, theelseblock will be executed.
- We create two
Save the file.
Now, let's compile and run the code.
Open the Terminal at the bottom of the WebIDE. Ensure you are in the
~/projectdirectory.Compile the Java files:
javac Printable.java Document.java InterfaceCheck.javaIf there are no errors, this command will create
Printable.class,Document.class, andInterfaceCheck.classfiles.Run the
InterfaceCheckprogram:java InterfaceCheckYou should see output similar to this:
obj1 implements Printable Printing Document: Important Report --- obj2 does not implement Printable
This output confirms that instanceof correctly identified that obj1 implements Printable while obj2 does not. Using instanceof before casting is crucial to prevent ClassCastException errors at runtime.
Test with Multiple Interfaces
In this step, we will extend our understanding of instanceof by working with multiple interfaces. A single class in Java can implement multiple interfaces, and instanceof can be used to check for each of them.
First, let's define another interface.
Open the WebIDE editor.
In the
~/projectdirectory, create a new file namedEditable.java.Open
Editable.javaand add the following code:package project; public interface Editable { void edit(String newContent); }This defines an interface named
Editablewith one method,edit().Save the file.
Now, let's modify our Document class to implement both Printable and Editable.
Open the
Document.javafile in the~/projectdirectory.Modify the class declaration to implement both interfaces:
package project; public class Document implements Printable, Editable { private String content; public Document(String content) { this.content = content; } @Override public void print() { System.out.println("Printing Document: " + content); } @Override public void edit(String newContent) { this.content = newContent; System.out.println("Document edited."); } }We've added
, Editableto the class declaration and provided an implementation for theedit()method.Save the file.
Next, let's modify our main class InterfaceCheck.java to test for both interfaces.
Open the
InterfaceCheck.javafile in the~/projectdirectory.Replace the existing code with the following:
package project; public class InterfaceCheck { public static void main(String[] args) { Object obj1 = new Document("Initial Content"); Object obj2 = "Just a String"; System.out.println("Checking obj1:"); // Check if obj1 is an instance of Printable if (obj1 instanceof Printable) { System.out.println("obj1 implements Printable"); Printable p1 = (Printable) obj1; p1.print(); } else { System.out.println("obj1 does not implement Printable"); } // Check if obj1 is an instance of Editable if (obj1 instanceof Editable) { System.out.println("obj1 implements Editable"); Editable e1 = (Editable) obj1; // Cast to Editable e1.edit("Modified Content"); // Call the edit method // After editing, let's print again to see the change if (obj1 instanceof Printable) { // We know it is, but demonstrating Printable p1_after_edit = (Printable) obj1; p1_after_edit.print(); } } else { System.out.println("obj1 does not implement Editable"); } System.out.println("---"); System.out.println("Checking obj2:"); // Check if obj2 is an instance of Printable if (obj2 instanceof Printable) { System.out.println("obj2 implements Printable"); // Printable p2 = (Printable) obj2; // Would cause ClassCastException // p2.print(); } else { System.out.println("obj2 does not implement Printable"); } // Check if obj2 is an instance of Editable if (obj2 instanceof Editable) { System.out.println("obj2 implements Editable"); // Editable e2 = (Editable) obj2; // Would cause ClassCastException // e2.edit("Some Content"); } else { System.out.println("obj2 does not implement Editable"); } } }In this updated code, we now check if
obj1is an instance of bothPrintableandEditable. SinceDocumentimplements both, bothifconditions forobj1will be true. We also demonstrate calling theedit()method after casting toEditable. Forobj2(theString), both checks will be false.Save the file.
Finally, compile and run the updated code.
Open the Terminal in the
~/projectdirectory.Compile the Java files again:
javac Printable.java Editable.java Document.java InterfaceCheck.javaRun the
InterfaceCheckprogram:java InterfaceCheckYou should see output similar to this:
Checking obj1: obj1 implements Printable Printing Document: Initial Content obj1 implements Editable Document edited. Printing Document: Modified Content --- Checking obj2: obj2 does not implement Printable obj2 does not implement Editable
This output shows that instanceof correctly identified that the Document object (obj1) implements both interfaces, and we were able to cast and call methods from both Printable and Editable. The String object (obj2) correctly showed that it implements neither.
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.javafile in the~/projectdirectory.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
Objectvariables:obj3is assignednull.obj4is assigned a new instance of the baseObjectclass, which does not implement our custom interfaces.
We then use
instanceofto check each of these objects against bothPrintableandEditable.Save the file.
Now, let's compile and run the updated code.
Open the Terminal in the
~/projectdirectory.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.javaRun the
InterfaceCheckprogram:java InterfaceCheckYou 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.
Summary
In this lab, we learned how to check if an object implements a specific interface in Java using the instanceof keyword. We started by defining a simple interface (Printable) and a class (Document) that implements it. We then created a main class (InterfaceCheck) to demonstrate how to use instanceof to verify if an object is an instance of the Printable interface. This fundamental technique is crucial for safely casting objects and calling interface-specific methods.
We further explored the flexibility of instanceof by testing objects that implement multiple interfaces and also considered edge cases such as handling null objects and objects that do not implement the target interface. This comprehensive approach ensures a robust understanding of how to reliably determine an object's interface implementation status in various scenarios.



