posted 21 years ago
void printS1S2(){printS1();printS2();}
is the same as
void printS1S2() { this.printS1(); this.printS2() );
I guess the confussion steems from not knowing wich class to look for printS1, either P , or Q?.
Always begin the search of the method to invoke by the class that is the compile type of of the expression on which the method invocation occurrs.
this is the expression on which the method invocation occurs. The compile type of this is the class declaring the method where this happens to be. That is P. Search P for printS1 declaration, there is one. Stop there, coz printS1 is static you do not have to look at subclasses.
Now, when printS2 is located in P, trying to resolve which printS2 method is called, we note that is not static, so we look at the runtime type of this. In this.printS2(), this is a reference to an instance of Q. Which one? the one created by new Q() in main. In the class Q there is a method overriding P.printS2, that method is called on the instance hold by this.
SCJP2. Please Indent your code using UBB Code