my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof Operator Question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof Operator Question." Watch "instanceof Operator Question." New topic

instanceof Operator Question.

Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Can anybody tell me why compiler is flashing error for the following code.
Error Msg "inconvertible types"
Actually it should print "no"
class Tree{
public void tell(){
public class Pine{
public static void main(String[] args){
Pine p=new Pine();
if (p instanceof Tree){

Oliver Grass
Ranch Hand

Joined: Nov 02, 2000
Posts: 65
Hi Prasad,
is it possible, that there is something missing
For example

Hope that helps
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Nothing is missing. Pine is not a subclass of Tree.
Udayan Naik
Ranch Hand

Joined: Oct 18, 2000
Posts: 135
Hi Prasad.The instanceof operator will give a compile-time error if the compile-time rules are not met.In this case class Pine does not extend class Tree,so the compiler will recognise that the runtime class of the object reference p is NEVER going to be Tree or any subclass of Tree.Hence the error.
Come on in !! Drinks are on the house in the Big Moose Saloon !!

Udayan Naik<BR>Sun Certified Programmer for the Java 2 Platform
Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 104
Thanks for this question - it's got me really stuck but I know a lot more about the instanceof operator than I did before. I haven't really got an answer but it would appear that there's some sort of compile time check to see if p's class and Tree are at least in the same class hierarchy somehow. So, as Oliver says, extending Tree in Pine works - butit makes the instanceof operator redundant anyway - we know all Pines are Trees so why bother checking?
If you get Tree to extend Pine (just to test the theory - it makes no sense from an OO viewpoint) and then recompile the code, everything works fine and when p is tested, "no" is output because this p is an instance of Pine, not Pine's subclass Tree.
Alternatively, if you leave Tree untouched and change
Pine p = new Pine();
Object p = new Pine();
everything works when recompiled and "no" is the output.
So I think, the compiler checks to see that p might be an instance of Tree. If p is an Object then it might be an instance of Tree because everything extends Object (Late binding so p is an Object at compile time even though it's actually an instance of Pine). If Pine extends Tree or Tree extends Pine then p might be an instance of Tree.
I can't find anyone who can confirm this / anything about this in the Java docs but it makes sense. You might want to know whether an Object retrieved from a Vector is an instance of a certain Class (Tree, Pine etc) but if you had an array of Pines would you ever want to check that they were Trees if the classes were completely unrelated?
I would be very interested if anyone knows whether there is a compile time check / any other info on this!
I agree. Here's the link:
subject: instanceof Operator Question.
It's not a secret anymore!