This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes output of program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "output of program?" Watch "output of program?" New topic
Author

output of program?

Hennry Smith
Greenhorn

Joined: Jan 21, 2011
Posts: 26
3. class Mammal {
4. String name = "furry ";
5. String makeNoise() { return "generic noise"; }
6. }
7. class Zebra extends Mammal {
8. String name = "stripes ";
9. String makeNoise() { return "bray"; }
10. }
11. public class ZooKeeper {
12. public static void main(String[] args) { new ZooKeeper().go(); }
13. void go() {
14. Mammal m = new Zebra();
15. System.out.println(m.name + m.makeNoise());
16. }
17. }





What is the result?
A. furry bray
B. stripes bray
C. furry generic noise
D. stripes generic noise
E. Compilation fails
F. An exception is thrown at runtime....


actually i know the answer i dont know why...please tell me the reason.....
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

So what is the answer? Some hints- Think about Overriding, Runtime polymorphism for methods and compile time binding for the variables. Runtime would see the instance type and compile time would see the reference type.


Mohamed Sanaulla | My Blog
Hennry Smith
Greenhorn

Joined: Jan 21, 2011
Posts: 26
Mohamed Sanaulla wrote:So what is the answer? Some hints- Think about Overriding, Runtime polymorphism for methods and compile time binding for the variables. Runtime would see the instance type and compile time would see the reference type.


the correct answer is A: furry bray

but i think in "void go()" method we have declared reference type of mammal class so furry wil b printed when we call "m.name" but how bray is printed....please explain.....i am confused....
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Hennry Smith wrote:

the correct answer is A: furry bray

but i think in "void go()" method we have declared reference type of mammal class so furry wil b printed when we call "m.name" but how bray is printed....please explain.....i am confused....

So for which variable to call would depend of the type of reference- which is Mammal. But for methods- If the method is overridden in Subclass, then the method invoked would depend on the type of the Instance- In the example- its Zebra. There have been lot of discussions on this topic in the forum. You can search for the same.
Hennry Smith
Greenhorn

Joined: Jan 21, 2011
Posts: 26
thanks ...i got it finally....
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

@Jacob Coddaire
I don't see how that reply contributes to the discussion.

@Hennry Smith
Please QuoteYourSources and please UseCodeTags when posting code. Don't use the quotes for that. Those will not highlight the code while code tags will.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: output of program?
 
Similar Threads
instance variable inheritance
Upcasting doesn't work as intended; clarification needed
ZooKeeper
Question in scjp study guide (Kathey & Bates) page 181- oo Concepts
I need to help with the question 13 chapter 2