Nidhi Sar wrote:Please take a look at the following code:
I would expect the output to be Parent's doStuff(), but actually it is Child's doStuff().
Why is it so? I understand the concept of polymorphic call to child's method if invoked on a child instance, but here it is just call from a constructor to a method of the same class. So why is it going to the child's method?
Thanks,
Nidhi
Because, even though it is the super class constructor, it is still a Child instance.
Java will find the most specific method it can, and run that one. Since you have a Child instance, which has its own doStuff() method, Java will use that one.
SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."