aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt about sub-super class relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt about sub-super class relationship" Watch "Doubt about sub-super class relationship" New topic
Author

Doubt about sub-super class relationship

Brandon Bay
Greenhorn

Joined: Aug 31, 2007
Posts: 29
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!
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
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)


SCJP-1.5<br />SCWCD-1.4
Brandon Bay
Greenhorn

Joined: Aug 31, 2007
Posts: 29
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!
Lucy Smith
Greenhorn

Joined: Aug 07, 2007
Posts: 17
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???
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt about sub-super class relationship