Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

output of program?

 
Hennry Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hennry Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ...i got it finally....
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic