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