But the statement
b = new A() instanceof I
is not generating compilation error
This is because it is theoretically possible for a new class, say class A1, to subclass the A class and implement I. And since, A1 IS-A A, this will be a case where A is a instanceof I.
Now, in this example, it is not possible, as A is actually instantiated on the same line. But the compiler is not smart enough to figure out -- maybe it will with future releases.
But the code
b = new A() instanceof D ;
is generating error.why?
Since A and D are not super/sub classes of each other, there is no way one could be an instance of the other. (since multiple inheritance is not supported with
Java)... So the compiler is smart enough to know that this statement will always be false, hence useless. (hence, compile error)
Henry