Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Weird behavior of instanceof

 
Sankaranarayanan Viswanathan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21413
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
maganti suryanarayana
Ranch Hand
Posts: 53
Java MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please send the complete code with output as an clipboard. I havenot understood it clearly.
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic