amol bakre wrote:Anyway, as a part of my prep for the certification i came across this question that i thought was interesting and kinda confusing and would welcome some explanations
And where did you find this code snippet and question? Always QuoteYourSources please.
Kamil Hlubek wrote:This is my first reply to explain somebody something and because I am preparing to my first exam ( OCAJP 8 ) , I hope much more you understand it and I did it well.
amol bakre wrote: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!!
amol bakre wrote:and now the program outputs "Polyparent" both times.
amol bakre wrote: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..
amol bakre wrote: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,
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote:b-Now at runtime jvm is presented with only object of extended class type,so it checks for the method in that object as described in bytecode,like matches return type etc
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote:By checks means i meant that at compile time compiler defines the method signature's information in bytecode and order jvm
to look for only that method that matches with the method signature as defined in bytecode.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote: I was always taught that if you want to explain something difficult to understand,then try to present in humorous way
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.