I made this class hierarchy in Java. Then I wrote this code in main method
This code is not generating any Compile time error. While interface1 and class3 are not related by any means i.e. they don't have a common sub type. Similarly interface1 and interface2 and interface2 and class2 are not related by any means. Then it must produce a compile time error. But it is not... Why is this so??? Is this some sort of bug in Java???
[ August 03, 2008: Message edited by: AnkitJi Garg ] [ August 03, 2008: Message edited by: AnkitJi Garg ]
First of all sorry for posting in the wrong section... I thought this is an advanced question....
Isn't it true that for compilation to succeed on the above instanceof operator type1 and type3 must have a common sub-type. If it is true then there must be a compilation error in the original code that I posted.. At run-time the code cannot return true in any circumstances. Then this must be detected by the compiler on compile time....
[ August 04, 2008: Message edited by: AnkitJi Garg ] [ August 04, 2008: Message edited by: AnkitJi Garg ]
You have misunderstood the workings of the instanceof operator. It does not require much of its left-hand operand; it can be any reference type, or even null (I don't know whether you can use a primitive to the left of instanceof). All that happens if you put something "wrong" to the left of instanceof is that it returns false.
More about instanceof here, and here. Those links suggest you can't use a primitive as an operand to instanceof.
This gives a compiler error: "Incompatible conditional operand types String and Integer"
posted 11 years ago
The above code will not compile, because it is impossible for a reference variable of type String to refer to an object that IS-A Integer. If it would compile, it would always return false. So it is a waste of CPU and the compiler will refuse to compile it.
But in the case of AnkitJi Garg, the case:
The expression in line 2 is not always necessarily false. Because there could be a class that extends class3 and implements interface1, like this:
Now the same line evaluates to true :
[ August 04, 2008: Message edited by: David Bala�ic ]
Thank you David Bala�ic. You really cleared my confusion... Now I got it that why in case of classes a compile time error is generated and not in case of interfaces. This is because you can only extend one class but you can implement multiple interfaces. So if you compare unrelated classes then it is sure at compile time that there can be no class that is a sub type of both the classes. But if you compare unrelated interfaces or an interface and a class, then the compiler cannot be sure as there can be a class that is a sub-type of both the interfaces. [ August 04, 2008: Message edited by: AnkitJi Garg ]
Very good question by ankitji. Could have been put into advanced forum. Though question deals on fundamentals of java, but for any practical purpose, i will not look for such puzzle questions in beginners level.
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad: