Forums Register Login

instanceof Operator Question.

+Pie Number of slices to send: Send
hi,
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(){
System.out.println("tree");
}
}
public class Pine{
public static void main(String[] args){
Pine p=new Pine();
if (p instanceof Tree){
System.out.println("yes");
}else{
System.out.println("no");
}
}
}
Regards
Prasad

------------------
+Pie Number of slices to send: Send
Hi Prasad,
is it possible, that there is something missing
For example

Hope that helps
cheers
Oliver
+Pie Number of slices to send: Send
Oliver,
Nothing is missing. Pine is not a subclass of Tree.
Regards
Prasad
------------------
+Pie Number of slices to send: Send
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 !!
+Pie Number of slices to send: Send
Prasad,
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();
to
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!
Kathy
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 845 times.
Similar Threads
instanceOf??
instanceOf
Question about 'instanceof'
mock question
Inheritance
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:54:38.