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 practice exam question 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 "practice exam question" Watch "practice exam question" New topic
Author

practice exam question

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95



Given:
Here is a sample problem (from Oracle 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.

The stated answer in D, however, I would have thought B. I would have figured that the call to (new Elem().getTree())) would have return elm, not tree.

Can anyone explain?
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
Elm inherits getTree method. When getTree is executed, it only knows tree variable is referring to "tree"
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Himai Minh wrote:Elm inherits getTree method. When getTree is executed, it only knows tree variable is referring to "tree"


I don't understand.

If Elm inherits getTree(), why won't it print Elm?

Would the answer change if the member variable tree was NOT static?
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
No, if tree is non-static, it still prints the same output.

When Elm inherits getTree, it calls the Tree's getTree () method, which returns Tree's tree.
I remember that polymorphism does not apply to variables.
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Himai Minh wrote:No, if tree is non-static, it still prints the same output.

When Elm inherits getTree, it calls the Tree's getTree () method, which returns Tree's tree.
I remember that polymorphism does not apply to variables.


ok, sorry if I am being difficult.

I think I have had a brain block. If the Elm class had overridden the getTree() method, then I believe new Elm().getTree() would return 'Elm'.

Since Elm did not OVERRIDE it, then the 'getTree() called is the one that was defined in the Tree class.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
To make the explanation simple, Elm instance reuses Tree's getTree method.
When getTree is executed, the Tree's tree variable is used as this "tree" is in the Tree's class.

Generally speaking, you can have another class OakTree extends Tree. But OakTree does not have its own tree="oak" instance.
So, when OakTree calls the getTree(), it executes Tree's getTree() method. Since OakTree does not have its own tree="oak", it uses Tree's "tree".

Polymorphism does not apply to variables.

Try this modified example:


The getTree() method will print out "elm".
Why? Because when getTree is reused by Elm, it calls the overrided getmyOwnTree. When Elm's getmyOwnTree is called, it prints out "elm".
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Himai Minh wrote:To make the explanation simple, Elm instance reuses Tree's getTree method.
When getTree is executed, the Tree's tree variable is used as this "tree" is in the Tree's class.

Generally speaking, you can have another class OakTree extends Tree. But OakTree does not have its own tree="oak" instance.
So, when OakTree calls the getTree(), it executes Tree's getTree() method. Since OakTree does not have its own tree="oak", it uses Tree's "tree".

Polymorphism does not apply to variables.

Try this modified example:


The getTree() method will print out "elm".
Why? Because when getTree is reused by Elm, it calls the overrided getmyOwnTree. When Elm's getmyOwnTree is called, it prints out "elm".


ok thank you, that makes sense
Joe Boman
Greenhorn

Joined: Dec 10, 2009
Posts: 20
I actually have a question about why the third output string named tree derives the value "elm".

If I am following correctly, Elm.java starts by instantiating an Elm object and calling Elm's go method with another instantiated Tree (base class) object that is named t inside the go method. As a result, inside the go method, two objects are accessible. Both have instance variables named tree. Why does it choose to use Elm's tree variable over the Tree class' tree variable?

Is it because referencing only the word tree is like saying this.tree? Whereas to maybe access Tree's tree variable would require a qualifier such as super.tree or t.tree? Thanks for bringing up the original question.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
In the previous example, it prints out tree elm elm elm from the go method.
The first one outputs tree from the invocation t.getTree().
Since t is a tree, it calls getTree and getTree calls getmyOwnTree.

The most interesting invocation is new Elm().getTree().
The Elm instances inherits getTree. This getTree calls getmyOwnTree of Elm's version.
In the Elm's getmyOwnTree, the "return tree" returns the Elm's tree.
Why getmyOwnTree in Elm choose "elm" to return instead of "tree" ?
It is because Elm's getmyOwnTree overrides its parent's method. That means Elm behaves differently than its parent.


Let's go back to the original example:


When Elm instance calls getTree(), it reuses its parent's getTree method. So, this Elm instance should behaves in the same way as its parent. Therefore, it returns "tree" instead of "elm".

Summary: a child inherits a method meaning that this child behaves in the same way as its parent. A child overrides a method meaning that this child behaves differently than its parents.
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Himai Minh wrote:In the previous example, it prints out tree elm elm elm from the go method.
The first one outputs tree from the invocation t.getTree().
Since t is a tree, it calls getTree and getTree calls getmyOwnTree.

The most interesting invocation is new Elm().getTree().
The Elm instances inherits getTree. This getTree calls getmyOwnTree of Elm's version.
In the Elm's getmyOwnTree, the "return tree" returns the Elm's tree.
Why getmyOwnTree in Elm choose "elm" to return instead of "tree" ?
It is because Elm's getmyOwnTree overrides its parent's method. That means Elm behaves differently than its parent.


Let's go back to the original example:


When Elm instance calls getTree(), it reuses its parent's getTree method. So, this Elm instance should behaves in the same way as its parent. Therefore, it returns "tree" instead of "elm".

Summary: a child inherits a method meaning that this child behaves in the same way as its parent. A child overrides a method meaning that this child behaves differently than its parents.


That is a good way to describe it. I guess I get confused because even though Elm is a superclass of tree, I see it that when you create an instance of Elm, there is only one object created (an Elm instance), and so there is only one method of getTree(), which seems to 'belong' to Elem since it was inherited. So only if Elm provided an overridden verison of getTree() would it return tree="elm", but since it did not override it, it would call the parent class.
Joe Boman
Greenhorn

Joined: Dec 10, 2009
Posts: 20
In the original example - the third output string +tree+ produced "elm ".
So did this produce "elm " because:
The go method was called using an Elm object, and the Elm object's tree variable was set to "elm "? Is that why the third string produced "elm "?
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
Hi Joe.
Yes.
Joe Boman
Greenhorn

Joined: Dec 10, 2009
Posts: 20
Thanks Himai for clarifying logic. I always fear answering a test question correctly but for the Wrong reason!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: practice exam question