• 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 operator

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would this code produce a compiler error @ line 1?

class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
public static void main (String[] args) {
Color color1 = new Red(); Red color2 = new Red();
boolean b1 = color1 instanceof Color;
boolean b2 = color1 instanceof Blue;
boolean b3 = color2 instanceof Blue; //1
System.out.print(b1+","+b2+","+b3);
}}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what the Java Language Specification says.

If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.

Since the reference type of color2 is Red, and you can't cast Red to Blue, you can't use the instanceof operator between color2 and Blue.
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its because they aren't in the same inheritance tree.


You can't cast like : (Blue)red_thing
[ April 06, 2006: Message edited by: Arno Reper ]
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically if both classes are siblings in the inheritance tree, they cannot neither be castEd nor compared with instanceof keyword. This situation produces a compile time error.

On the other hand, this rule is not valid when casting or comparing via instanceof keyword classes to interfaces and vice-versa. Unless the class is marked as final, you can cast or compare with instanceof keyword any class to any interface and vice-versa.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is

true, false, false.

It is clear that the object of red is object of color
and
object of red is not object of blue
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Vidhya,
The line color2 instanceof Blue produces compile time error because whenever u use a instanceof operator with right hand side argument as a class the compiler will check if there is any inheritance relationship between the reference on the left hand side and the class on the right hand side. If there is no such reation then error is produced.
in this case color2 is a reference of Red class and there is no inheritance relationship between Red class and Blue( that is neither is the superclass of other). Hence a compiler error is produced.
Please let me know if further clarifications are needed.
 
Vidhya Hari
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ....Those were really good explanations
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic