• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!