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.
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.