SCJP2. Please Indent your code using UBB Code
Originally posted by Manish Hatwalne:
[B]I think all options are incorrect.
There are 3 print statements, so o/p will be -
<pre>
From Parent
From Parent
From child
</pre>
I tested this and did indeed get the above output. The third print I understand. However when your say:
"moreover print() method in OverRidingParent class is private, hence all function calls get resolved at comile time and do not display any polymorhic behaviour at runtime."
I don't get this! I thought that java used late-binding for method calls and with the print method being private how can it be called from outwith the object (or is this possible here because the main method is in the class). Before I ramble on more could someone clarify further?
scott
cheers,<br /> <br /> Scott
"moreover print() method in OverRidingParent class is private, hence all function calls get resolved at comile time and do not display any polymorhic behaviour at runtime."
I don't get this! I thought that java used late-binding for method calls ...
Private methods are neither inherited, nor overriden. They staticly bind (early binding) to the the declared type variable (p1 here in first 2 cases) at compile time. Late binding happens with overriding. I think, Jose explained it quite clearly.
...and with the print method being private how can it be called from out with the object (or is this possible here because the main method is in the class).
Right, that's the reason. main happens to be a static method of the class, besides being an entry point.
Anyway its very important to look for the declaration of the method first in the declared type (compile type) or supertypes of the variable, not in the type of the reference pointed to by the variable.
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|