This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

KB chapter 5 q.21  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I would be grateful if someone would explain Q.21 from KB book chapter 5, don't understand why Foo Bar Foo etc (option B) is the answer. I see that getFoo method is returning a Foo reference but thought it would decide what method to call based on the object type, i.e. when its a bar object call the react method in bar? Would really appreciate a simple explanation to this. Taking the exam tomorrow!
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sharon hhh,

Welcome to Javaranch

We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.

I'd love to help with your question but I do not have that book (and I'm guessing that many folks here don't have it, as well). If you can reproduce the question so that I can see what you're talking about, I might be able to help.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by sharon hhh:
Hi, I would be grateful if someone would explain Q.21 from KB book chapter 5, don't understand why Foo Bar Foo etc (option B) is the answer. I see that getFoo method is returning a Foo reference but thought it would decide what method to call based on the object type, i.e. when its a bar object call the react method in bar? Would really appreciate a simple explanation to this. Taking the exam tomorrow!



The react method is overridden and as such, the method that will be called will be determined at runtime, depending upon the type of object created. Since there are alternating Foo and Bar objects being created in the question, you get answer B.

If the react method was overloaded, then it would call the method as determined at compile time, which would be answer C.

Make sense?
 
Everybody's invited. Except this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!