This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceOf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceOf" Watch "instanceOf" New topic


Layla Rashid
Ranch Hand

Joined: Dec 25, 2006
Posts: 36
Here I got the output Pine Tree Oops , I am wondering why tree is a instance of pine . Pine is a subclass of Tree And if I get output Pine then why I don't get output Oak ? This is 32 number question from MindQ.

class Tree{}
class Pine extends Tree{}
class Oak extends Tree{}
public class Forest
{ public static void main( String[] args )
{ Tree tree = new Pine();

if( tree instanceof Pine )
System.out.println( "Pine" );

if( tree instanceof Tree )
System.out.println( "Tree" );

if( tree instanceof Oak )
System.out.println( "Oak" );

else System.out.println( "Oops" );

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

There are two things being created here. You create a reference variable, that can only point to a Tree. You then create an object, that is a Pine. Since a Pine IS-A Tree, you are allowed to use the Tree reference to point to it - but the object remains a Pine.

You can only call the methods that are defined for Tree. It's like if you have a remote control for a basicSonyTV. If you buy a fancySonyTv, you can still use the remote to turn it on and off, change channels, and adjust the volume. However, you cannot use your old remote to control picture-in-picture - the buttons simply are not there. The fancySonyTV has the ability to do pic-in-pic, but you can't get to it.

Now, back to your code.

When you use the instanceof operator, it looks at what the OBJECT is, not the reference. So, since the Object really IS a pine, we print Pine.

Now, since Pine extends Tree, all Pines are Trees. So, when the instanceof operator looks at the object, we see it's a Pine, and therefore a Tree, so we print Tree.

Our actual object, however, is NOT an Oak, so we go into the else, and print "Oops".

I think the key point is that the OBJECT is a Pine, but the REFERENCE is a tree. The instanceof operator looks at what the OBJECT is.
[ December 29, 2006: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ramya Chowdary
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
1) tree instanceof Pine. gives true

here tree reference points to Pine it prints true.

2)tree instanceof Tree gives true.
as Pine object is subclass of Tree,we can cast to Tree type without any problem

3) tree instanceof Oak. gives false

as pine Object and Oak are different.

I agree. Here's the link:
subject: instanceOf
It's not a secret anymore!