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?
Object obj = new B()
, it compiles. – Cartelizeclass B
isfinal
thenobj instanceof C
will not compile either, because ifB
can have no subtypes, then it is guaranteed to be unrelated toC
. – Sosthina