Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof Operator Question.

 
Prasad Ballari
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

------------------
 
Oliver Grass
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prasad,
is it possible, that there is something missing
For example

Hope that helps
cheers
Oliver
 
Prasad Ballari
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oliver,
Nothing is missing. Pine is not a subclass of Tree.
Regards
Prasad
------------------
 
Udayan Naik
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
Kathy Rogers
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic