And where did you find this code snippet and question? Always QuoteYourSources please.
noted, I found a similar question on one of the tests of enthuware.
I kind of figured why a '0' was printed and later a 10. What i cant get my head around is why the call to print() from contructor of Parent goes to print() of Child. Why is dynamic dispatch even being considered here as there we are not calling print() on any object from within the constructor!!
I changed the program to read:
and now the program outputs "Polyparent" both times.
So i'm guessing, the compiler has checked that print is a valid overridden method, object type is Child and the runtime has adhering to polymorphism called on the Child's print..
What if i had an init() method in both my classes <for some reason i did not want to use initialization blocks, but init() methods> then i will not be able to initialize my superclass to how i want that to be initialized,
But then i am thinking probably I would only override because i wanted to alter the behaviour in the parent, so this, what's happening is correct, i should just make a call to super.print() (from within Child.print()) if there was some importaint peice that needed to be executed.