• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

OCP 17 Study Guide Chapter 7 Review Question 20

 
Greenhorn
Posts: 25
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
The question asks to determine if that statement is true or false:

Given an arbitrary instance of a class, it cannot be determined until runtime which overridden method will be executed in a parent class.



and the answer is true:

Part of polymorphism is that methods are replaced through overriding wherever they are called, regardless of whether they're in a parent or child class. For this reason, option A is correct,



I might don't understand the question and the meaning of "arbitrary instance".

It's very clear to me how overriding works, and I would say that all of overridden methods called inside the parent class will be executed, despite the reference type of the instance. So we already know before runtime.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Marco, let's take a look at it with an example. Let's say you have this parent class:


Now looking only at the above code, can you say for sure whenever print method is called it will always print Message: Parent class? I might call the call method with new Parent() as parameter OR I might send a new Child() to the call method. Or I might create another class which extends Parent. I'm pretty sure that's what the question meant to say. When you look only at your class (Parent in this case), you don't know who will inherit from it (someone else 6 months later might inherit from your class), so you cannot be sure which method call will be overridden.
 
Marco Olivi
Greenhorn
Posts: 25
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Ankit!
Now I understand what the question was asking for, and the meaning of "arbitrary instance".
Thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic