1

I am working with Java and trying to use 'instanceof' to determine if an object returned is an instance in a class. The code (not my code) I have to work with is layed out in this manner. The question is why instanceof cannot cast correctly to determine if the object is an instance of the class?

public class MainClass{
    public static void main(String[] args){
        Class1 myClass1 = new Class1();
        if(myClass1.getObject() instanceof Class1){}  ///<--- Cast Error
    }
}


public class Class1 extends ObjectClass{

    public InnerClass1 getObject(){
        return (InnerClass1)object;
    }

    public static abstract class InnerClass1 extends Class2{}
}


public class Class2 {}


public class ObjectClass {
    final Class2 object = new Class2();
}

As you can see the object is an instance of Class2. When calling the 'get' method it casts the object to type InnerClass1 which extends Class2. InnerClass1 is a subclass of Class1. The 'instanceof' check wants to check if the object is an instance of Class1 but it fails with a cannot cast error. Why is this? Thanks for help.

---EDIT--- Just a reminder, this is NOT my code. I was given this to work with and was just having some difficulty parsing through why the instanceof was not working. The answers were great and I understand now. Thanks!

3
  • 3
    Class2 is not subclass of InnerClass1. Commented Feb 2, 2018 at 21:14
  • Adding to what @tsolakp said, Class2 is not a subclass in the first place. Commented Feb 2, 2018 at 21:15
  • If you perform your getObject method first to a temporary reference, like: Object tempObject = myClass.getObject(); you would see where the error is coming from. Try not to convolute your code to much as it often the cause of such misunderstandings as you now have. Commented Feb 2, 2018 at 21:17

1 Answer 1

1

InnerClass1 is a subclass of Class1

No InnerClass1 extends Class2 not Class1, also Class2 doesn't extend any class, so it can't be explicit nor implicit subtype of Class1 which means above quote is incorrect.


Anyway at

myClass1.getObject() instanceof Class1 

you are checking if result of myClass1.getObject() is subtype of Class1.

But compiler knows that getObject() is declared as

public InnerClass1 getObject(){..} 

so it must return instance of InnerClass1 or its subtype. But since InnerClass1 and Class1 are not related and they can't have common ancestor nor common child (there is no multiple inheritance in Java for classes so any subclass can have only one parent class) it is impossible to have instance of InnerClass1 (or its subtype) which will also be of type Class1.

So since such instance can't exist (I am not including null here) compiler doesn't let you write code based on assumption that such instance may exist.

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

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.