• 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

Doubt about sub-super class relationship

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Taken from Sun e-Practice SCJP Java 5 Mock Exam





What is the result?

A. elm elm elm elm
B. tree elm elm elm
C. tree elm tree elm
D. tree elm elm tree
E. Compilation fails.
F. An exception is thrown at runtime.



Answer : Option D is correct. These are all valid ways to access the type and supertype's static variables.

The part which puzzles me is the "(new Elm().getTree());" part, why does it return the String "tree" instead of "elm" ? Since the object refers to Elm, and should take Elm's variable.

Thanks for any feedback ranchers!
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Threre is no getTree() method in Elm class, so when you call new Elm().getTree(), it inherits getTree() method from the Tree class.(Elm is a subclass of Tree, so it can inherit)
 
Brandon Bay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, yeah i understand that part...

However, the weird thing is that why did the method return the variable from the superclass instead of subclass ? Since it was a instance of Elm which called the method, shouldnt it take the String variable from Elm ?

Thanks dolly!
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brandon,

I'm no big expert but in this case I think I can explain.

In new Elm().getTree(), you are actually calling a methos from super class and that method is supposed to return that class's instance variable. The name of the variable being the same is causing the confusion and you must know about shadowing.

I changed the class as follows:

class Tree {
private static String tree = "tree ";
private static String tree1 = "tree1 ";
String getTree() { return tree; }

String getTree1() { return tree1; }
}

class Elm extends Tree {
private static String tree = "elm ";
public static void main(String [] args) {
new Elm().go(new Tree()); }
void go(Tree t) {
String s = t.getTree() + Elm.tree + tree + (new Elm().getTree1());
System.out.println(s); } }

now the output is: tree elm elm tree1

It's just you are calling super class method and it returns the super class's instance variable.

makes sense???
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brandon Bay:
Hmm, yeah i understand that part...

However, the weird thing is that why did the method return the variable from the superclass instead of subclass ? Since it was a instance of Elm which called the method, shouldnt it take the String variable from Elm ?

Thanks dolly!




No.
The method is not overridden in Elm. Therefore it can only return the field from the super class.

Condendsed example:

the output is "A".


By the way: "Tree" is not a taxonomic unit.
Class "Tree" should be renamed to "Planta", "Magnoliophyta", "Magnoliopsida", "Rosales" or "Ulmaceae"


Bu.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy "Lucky Lucy" !

Thanks for one of your first contributions to this forum and...

Welcome to the Ranch!




Hope you'll enjoy.




Only one small issue: The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp


So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.


I'm posting this because I am one of the moderators of this forum.


Yours,
Bu.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic