1

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.

2
  • 1
    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) Commented Oct 25, 2012 at 10:27
  • Wow, it appears to take pretty precisely 7 minutes to answer this question. Commented Oct 25, 2012 at 10:31

5 Answers 5

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.