File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

John Mathew
Greenhorn

Joined: Apr 17, 2007
Posts: 13
32. Consider the following code sample:

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" );
}
}

Select all choices that will be printed:

a) Pine
b) Tree
c) Forest
d) Oops
e) (nothing printed)
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Pine
Tree
Oops

Apply the IS-A methodology for each case.

For more about Inheritance and IS-A, please check Sun's Inheritance tutorial ...

Best of luck ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Tommaso Nuccio
Ranch Hand

Joined: Dec 11, 2006
Posts: 66
John,

what is the actual question?

As Vassili said
The "tree" object is of type "Pine".
Pine extends Tree, thus Pine IS-A Pine itself and Pine IS-A Tree.
Pine IS-NOT-AN OAK, that's why it prints "Oops" instead of "Oak".

Good luck.


Ciao,<br /> Tommaso<br /> <br />~*~*~*~<br />There are 10 types of people, those who understand binary and those who don't.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance