• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Weird behavior of instanceof

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 53
MyEclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please send the complete code with output as an clipboard. I havenot understood it clearly.
 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic