wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Weird behavior of instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Weird behavior of instanceof" Watch "Weird behavior of instanceof" New topic
Author

Weird behavior of instanceof

Sankaranarayanan Viswanathan
Greenhorn

Joined: Sep 27, 2011
Posts: 5
In the below code:



Why does the instanceof operation not result in a compile time error? If Test2 and Test3 were classes instead of interfaces, then the code does result in a compile time error.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

Sankaranarayanan Viswanathan wrote:In the below code:



Why does the instanceof operation not result in a compile time error? If Test2 and Test3 were classes instead of interfaces, then the code does result in a compile time error.


If test2 and test3 were class definitions, it is possible for the compiler to determine that there is no way that an instance that IS-A test2 can also be IS-A test3. However, if test2 and test3 are interfaces, it is possible to have an instance that IS-A test2 and IS-A test3, and hence, not possible for the compiler to flag it as a test that can never return true.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
maganti suryanarayana
Ranch Hand

Joined: Mar 30, 2010
Posts: 53

please send the complete code with output as an clipboard. I havenot understood it clearly.


surya
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

There is error in line 05.
I didn't understand your code.

this is what your code want to say :

interface Jkl{}
interface Pkl{}
class InterfaceTest implements Jkl
{
public static void main(String... arrg)
{
Jkl ob = new InterfaceTest();
System.out.println( ob instanceof Pkl);
}
}


In that case too there instanceof should result in error.
But it return false.
Petr Vasicek
Greenhorn

Joined: Jan 04, 2006
Posts: 5
The only thing that compiler knows it's that "ob" is implementation of Jkl. And since there can be implementations of Jkl that will also implement Pkl the compilation succeeds.

The compilation succeeds even in the case your declaration is "InterfaceTest ob". That's because InterfaceTest is not declared final, therefore there can be a class that extends it and that implements Pkl. Therefore there's a chance that an object referenced by InterfaceTest (extending it) will implement Pkl and compiler will not complain.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


At line #1 compiler can determine that class Alone does not implement Jkl interface .
Then Still it return false , not error . WHY ?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Weird behavior of instanceof
 
Similar Threads
Error: Cannot find symbol during Importing package
Interface doubt
instanceof doubt
protected field
instanceof Q? for SCJP