• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic