Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 23813
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: 62846
203
  • 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.
     
    Poop goes in a willow feeder. Wipe with this tiny ad:
    Programmatically Create PDF Using Free Spire.PDF with Java
    https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!