• 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

Question about instanceof

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The mock exam is from JavaChampion.com, question 8 of Inheritance test. I deleted some lines that are irrelevant to my question.

I did wrong in the above mock exam question. When I executed the program in eclipse, I got more confused than before.
If right operand is a class and the object is not in the same inheritance tree, compilation error occurs. I understand this part.
If right operand is an interface and the object does not implement the interface, no compilation error occurs and the program prints out "false".

I don't get why one statement causes compilation error and the other returns a boolean value. Can someone explain it to me please?

Thanks,

Connie



 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


instanceof operator act much like as casting expression.
example:


compiler assume there might be a class which is subclass [to an A] which implements I.

<edit>I think you can join this long discussion : interface-typecasting </edit>

hth
[edited for spelling mistake]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Intuitively, as a class cannot extend but a class, there can be an 'inheritance tree' that can be checked by the compiler.
As a class can implement several interfaces, and its super-classes can implement several interfaces, and the interfaces themselves can extend several interfaces, and the the Individual reference can be instance of any of its subclasses (which adds more to the search), therefore situation can get messy and I suppose it is best if it's solved at run time (when you know exactly what kind of object you have)...

But this is only a guess, so I, too, am waiting for a technical answer
 
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

First please QuoteYourSources -- which mock exam did this come from?

Connie KY Leung wrote:
I don't get why one statement causes compilation error and the other returns a boolean value. Can someone explain it to me please?



In the second case, where false was returned -- it is basically because the compiler could not determine, in a guaranteed manner, that an instance of Individual is not an instance of Colorable.

By just looking at the code, you can argue that the compiler is just being stupid -- and I would agree. But here is why it is allowed... It is possible for a class to extend Individual that implements Colorable, and hence, possible that a class that IS-A both Individual and Colorable. From the way the code is written, it's not possible for the comparison, but the compiler isn't that smart.

Henry
 
Connie Ky Leung
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
First please QuoteYourSources -- which mock exam did this come from?

Connie KY Leung wrote:
I don't get why one statement causes compilation error and the other returns a boolean value. Can someone explain it to me please?



In the second case, where false was returned -- it is basically because the compiler could not determine, in a guaranteed manner, that an instance of Individual is not an instance of Colorable.

By just looking at the code, you can argue that the compiler is just being stupid -- and I would agree. But here is why it is allowed... It is possible for a class to extend Individual that implements Colorable, and hence, possible that a class that IS-A both Individual and Colorable. From the way the code is written, it's not possible for the comparison, but the compiler isn't that smart.

Henry



Source updated in the original post. Thanks.
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also see results when class Individual is marked final......
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic