Ajay Sharma
Ajay Sharma

Reputation: 868

The 'instanceof' operator behaves differently for interfaces and classes

I would like to know regarding following behavior of instanceof operator in Java.

interface C {}

class B {}

public class A {
    public static void main(String args[]) {
        B obj = new B();
        System.out.println(obj instanceof A);      //Gives compiler error
        System.out.println(obj instanceof C);      //Gives false as output
    }
}

Why is it so? There is no relation between interface C and class B, but it gives false whereas in case of obj instanceof A it gives compiler error?

Upvotes: 92

Views: 4802

Answers (3)

gleitonfranco
gleitonfranco

Reputation: 799

I'm not sure about which Java version you are concerning to, but sealed interfaces/classes (>=17) are a way of detecting this in compiling time. In this example is explicit D the only class could implement C, and A or B not:

sealed interface C permits D {}

class B {}

final class D implements C {}

public class A {
    public static void main(String args[]) {
        B obj = new B();
        System.out.println(obj instanceof A); //Gives compiler error
        System.out.println(obj instanceof C); //Gives ANOTHER compiler error
    }
}

See: https://docs.oracle.com/en/java/javase/17/language/sealed-classes-and-interfaces.html

Upvotes: 1

Nurettin Armutcu
Nurettin Armutcu

Reputation: 365

By using the final modifier in the class declaration below, it is guaranteed that there couldn't be a subclass of Test, which may implement the interface Foobar. In this case, it is obvious that Test and Foobar are not compatible with each other:

public final class Test {

    public static void main(String[] args) {
        Test test = new Test();
        System.out.println(test instanceof Foobar); // Compiler error: incompatible types
    }
}

interface Foobar {
}

Otherwise, if Test is not declared final, it might be possible that a subclass of Test implements the interface. And that's why the compiler would allow the statement test instanceof Foobar in this case.

Upvotes: 2

Tagir Valeev
Tagir Valeev

Reputation: 100239

Because Java has no multiple class inheritance it's absolutely known during the compilation that obj object of type B cannot be subtype of A. On the other hand it possibly can be subtype of interface C, for example in this case:

interface C {}

class B {}

class D extends B implements C {}

public class A {
    public static void main(String args[]) {
        B obj = new D();
        System.out.println(obj instanceof C);      //compiles and gives true as output  
    }
}

So looking only at obj instanceof C expression compiler cannot tell in advance whether it will be true or false, but looking at obj instanceof A it knows that this is always false, thus meaningless and helps you to prevent an error. If you still want to have this meaningless check in your program, you can add an explicit casting to the Object:

System.out.println(((Object)obj) instanceof A);      //compiles fine

Upvotes: 132

Related Questions