java - Errors in using instanceof operator with an interface?

I am trying to learn about instanceof operator in java as per the link :- instanceof

But when i try to run their following code:-

class InstanceofDemo {
public static void main(String[] args) {

    Parent obj1 = new Parent();
    Parent obj2 = new Child();

    System.out.println("obj1 instanceof Parent: "
        + (obj1 instanceof Parent));
    System.out.println("obj1 instanceof Child: "
        + (obj1 instanceof Child));
    System.out.println("obj1 instanceof MyInterface: "
        + (obj1 instanceof MyInterface));
    System.out.println("obj2 instanceof Parent: "
        + (obj2 instanceof Parent));
    System.out.println("obj2 instanceof Child: "
        + (obj2 instanceof Child));
    System.out.println("obj2 instanceof MyInterface: "
        + (obj2 instanceof MyInterface));
}

}

class Parent {}
class Child extends Parent implements MyInterface {}
interface MyInterface {}

I get the following errors at compile time:

./Child.java:1: error: cannot find symbol
class Child extends Parent implements MyInterface{
                                      ^
  symbol: class MyInterface
InstanceOfDemo.java:9: error: cannot find symbol
System.out.println("obj1 instanceOf MyInterface" + (obj1 instanceof MyInterface));
                                                                    ^
  symbol:   class MyInterface
  location: class InstanceOfDemo
InstanceOfDemo.java:12: error: cannot find symbol
System.out.println("obj1 instanceOf MyInterface" + (obj2 instanceof MyInterface ));
                                                                    ^
  symbol:   class MyInterface
  location: class InstanceOfDemo
3 errors

Below is the directory structure where the src code is placed:-

-rw-rw-r-- 1 ankit ankit 174 Oct 25 15:36 Child.class
-rw-rw-r-- 1 ankit ankit  31 Oct 25 15:36 Child.java
-rw-rw-r-- 1 ankit ankit 920 Oct 25 15:41 InstanceOfDemo.class
-rw-rw-r-- 1 ankit ankit 637 Oct 25 15:41 InstanceOfDemo.java
-rw-rw-r-- 1 ankit ankit  25 Oct 25 15:20 MyInterface
-rw-rw-r-- 1 ankit ankit 186 Oct 25 15:36 Parent.class
-rw-rw-r-- 1 ankit ankit  16 Oct 25 15:18 Parent.java

Note: I removed the interface implementation from Child.java and InstanceOfDemo.java to compile and run the code.

Edit 1:- can i consider that instanceof operator can't be used with Interfaces because the definiton of instanceof operator as per Oracle is as follows:-

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

5 Answers

  1. Larry- Reply

    2019-11-13

    Your interface should also be in .java file so you need to put MyInterface in MyInterface.java

    -rw-rw-r-- 1 ankit ankit  25 Oct 25 15:20 MyInterface<---Rename it as 
                                                             MyInterface.java
    

    After this compile again and everything should work fine

  2. Laurent- Reply

    2019-11-13

    Looking file listing in question, your MyInterface is missing .java extension.

    instanceof can most certainly be used with interfaces.

    If you wonder about "or an instance of a class that implements a particular interface" part, it means that myObj instanceof AnInterface tests, if myObj is an instance of a class that implements a particular interface, namely AnInterface.

  3. Lawrence- Reply

    2019-11-13

    MyInterface seems to be defined in a different file, and the name of the file isn't even MyInterface.java. Hence the .class file isn't generated(and therefore isn't being found by the compiler)

    Solution is to rename the file MyInterface to MyInterface.java and compile it. This will generate a .class file, which is loaded by your JVM.

  4. Leander- Reply

    2019-11-13

    Rename MyInterface to MyInterface.java. Both your directory listing and error messages agree that it was never compiled to a class file.

  5. Lee- Reply

    2019-11-13

    Rename your file MyInterface to MyInterface.java then your code will run

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>