• 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

instanceof

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A question from one of chisholm's mock


That's understandable.. say I change the code by declaring an interface
"interface iface {}" and replacing the line3 with the following
"boolean b3 = color2 instanceof iface;". Output is true,false,false.
Why is compiler not complaining? Does this mean "instanceof" behaves differently for interfaces?

Thanks,
Neel.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interfaces and classes are behaved the same for the instanceof operation.

Your case is that, during the compilation time, in line 3, the compiler already knows color2 (as you declared it as Red) is NOT an instance of BLUE (as BLUE does not extend from RED), hence, you get the compilation error.

For line 2, as color1 is declared as COLOR only, the compiler does NOT know whether it is RED or BLUE, and thus, it left to the runtime engine (JVM) to determine this.

Same case for the interface case.

Nick
 
neel sri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!! Nick..
I did understand why compiler is throwing an error for this
boolean b3 = color2 instanceof Blue;
But my question is why whould it not throw an error
boolean b3 = color2 instanceof iface; ( Assuming that iface is an interface declared in the same file).

For that matter any interface compiles fine in the place of Blue in line "boolean b3 = color2 instanceof Blue;" but not any class other than "Red" and "Color".

boolean b3 = color2 instanceof Collection; also compiles fine!! why??

Hope I'm clear this time.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The trick here is the compiler knows the reference type of the Object, in this case Red. The compiler knows that this object is an Object of type Red or a subclass of red. It is impossible for any Object of type Red to also be of type Blue, but it is possible for a subclass of Red to implement the interface iface (or any other interface) so the compiler is unable to rule out that possiblility.
 
neel sri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steven, thank you!!
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's an interesting post.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic