The way I see this one is like this:
p.method2();
is a call of method2() using a Parent reference that refers to a Child object. Since Child did not implement a method2() the only one that we have was inherited from Parent, so it gets called and we get the printout
"Parent's method2()". After the print a call is made to method1(). The question is, which one? Well, the method1() in Parent is private so Child can NOT inherit it. However, Child has implemented a method1() with the same signature, same return type, but it is marked public. But be very clear.... this is NOT overriding. You can not override a private method.
So what decides which version gets called? The type of the reference variable that we used to call in with which, in our case, is Parent. Therefore we wind up calling the Parent version of method1() printing out "Parent method1()".
UPDATE
I should have prototyped, THEN commented.
Apparently, when the method1() call is within the suprclass method2() it will always call the parent method1().
I prototyped your
test and added a Child c = new Child(); and c.method2(); and it still calls Parent method1() on both lines.
HOwever, when I then added a c.method1() directly, it called the child version.
My bad.
[ September 09, 2008: Message edited by: Bob Ruth ]