aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instance of operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instance of operator" Watch "instance of operator" New topic
Author

instance of operator

suneeta prattipati
Greenhorn

Joined: Oct 16, 2000
Posts: 8
can anyone please explain what happens when the foll code is run
class Tree{}
class Pine extends Tree{}
class Oak extends Tree{}
public class Forest
{ public static void main( String[] args )
{ Tree tree = new Pine();//i can't understand what this line is doing
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" );
}
}
Select all choices that will be printed:
a) Pine
b) Tree
c) Forest
d) Oops
e) (nothing printed)
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Dear Sunita,
When the code mentioned is run the output is a,b,d
Now why does this happen???Lets see,
Tree tree = new Pine();
This line creates an object refrence "tree" of Pine which points to a new Object which has features common in Tree and Pine.
it is quite obvious that tree is instanceof Pine,and because Pine extends Tree,so tree is also an instanceof Tree indirectly....
tree has nothing to do with Oak.......coz it is a different class alltogather...
it prints Oops also coz else is only associated with
if( tree instanceof Oak )
System.out.println( "Oak" );
this 'if ' returns false and hence else part is also printed i.e. Oops
else System.out.println( "Oops" );
Hope you got it.......
Harpal
Aqeel Ahmad
Greenhorn

Joined: Sep 23, 2000
Posts: 11
I think the confusion here is regarding the inheritance relations. Sinc Oak and Pine both extends Tree. So they are both Tree(s) and can be treated like Tree. in line
<code>Tree tree = new Pine();</code>
Since Pine is also a Tree so it can be referenced as an instance of class Tree.
<code>(tree instanceof Tree)</code> is right because tree no doubt is a Tree
<code>(tree instanceof Pine)</code> is not right because not every Tree is necessarily a Pine.
[This message has been edited by Aqeel Ahmad (edited October 21, 2000).]


Aqeel
suneeta prattipati
Greenhorn

Joined: Oct 16, 2000
Posts: 8
hi Harpal,
Great explanation. I got it. Thanks a ton and wish you all the best for SCJP.
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229

Suneeta,You are welcome.....The pleasure was entirely mine..All the best to you too for SCJP.....
Take care and work hard,
Harpal
midrisi
Greenhorn

Joined: Oct 22, 2000
Posts: 7
Originally posted by suneeta prattipati:
can anyone please explain what happens when the foll code is run
class Tree{}
class Pine extends Tree{}
class Oak extends Tree{}
public class Forest
{ public static void main( String[] args )
{ Tree tree = new Pine();//i can't understand what this line is doing
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" );
}
}
Select all choices that will be printed:
a) Pine
b) Tree
c) Forest
d) Oops
e) (nothing printed)

As others fellow already told you that the ans will be a),b),d).
In the following statement
Tree tree = new Pine();
there is concept called object conversion rules has been applied
as it states that an reference of subclass can be converted to refernce of parent from which it has been inherited.Only upward move is allowed i.e. A parent reference cannot converted or type cast to sublcass reference.For more info refer to Robert And Heller book (topic is PRIMITIVE AND OBJECT REFERENCE)
I think this may solve your query.
Smile
Sharif

[This message has been edited by midrisi (edited October 22, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instance of operator
 
Similar Threads
MindQ's mock 32
Inheritance
instanceOf??
mock question
Doubt in instanceof