File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
John Mathew
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1585
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic