This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • 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

Private method overiding  RSS feed

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

OCAJP - 1 book assessment test has below program. I did not understand how could parent access its private method when parent reference instantiated child object.



ouput is deer childCH false.

I am expecting deer childCH true. According to run-time polymorphism, child class hasHorns has to be invoked, right? Please let me know. Thanks.
 
author
Posts: 23810
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Private methods are not overridden -- no polymorphism.

Henry
 
Marshal
Posts: 61805
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please supply more details of the book: is it by Boyarsky and Selikoff?

Please follow the execution through the constructors;-
  • 1: ChildCH constructor calls no‑arguments superclass constructor.
  • 2: ConstructorHierarchy constructor calls its superclass' constructor (=Object()).
  • 3: Object() completes and control passes back to ConstructorHierarchy() which prints “deer”
  • 4: ConstructorHierarchy() ocmpletes and control passes to ChildCH() which prints “ChildCH”
  • 5: Line 21 completes.
  • Now,, you can access the private method because you are inside the same class. The hasHorns() method is not inherited by ChildCH; it exists only in ConstructorHierarchy. Since there is no polymorphism involved, the type of the method is bound statically at compile‑time on the declared type of the variable, i.e. ConstructorHierarchy. The method with a similar name in the subclass is not an overriding method.

    Private methods are not inherited. The method in the subclass is a new method completely unrelated to the superclass' method. So it is not called polymorphicaally and the superclass method prints false.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!