FROM:
www.danchisholm.net/august04/topic/inherit.html class R {
private void printS1(){System.out.print("R.printS1 ");}
protected void printS2() {System.out.print("R.printS2 ");}
protected void printS1S2(){printS1();printS2();}
}
class S extends R {
private void printS1(){System.out.print("S.printS1 ");}
protected void printS2(){System.out.print("S.printS2 ");}
public static void main(
String[] args) {
new S().printS1S2();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: R.printS1 R.printS2
b. Prints: R.printS1 S.printS2
c. Prints: S.printS1 R.printS2
d. Prints: S.printS1 S.printS2
e. Runtime Exception
f. Compiler Error
g. None of the Above
ANSWER: B
EXPLANATION:
Prints: R.printS1 S.printS2 Method R.printS1 is private and therefore is not inherited by class S and is not overridden by method S.printS1. In contrast, method R.printS2 is protected and therefore is inherited by class S and is overridden by method S.printS2. R.printS1S2 therefore calls the R.printS1 and S.printS2 methods.
----
I was confident the answer was D. According to the explanation above:
"Method R.printS1 is private and therefore is not inherited by class S and is not overridden by method S.printS1."
I agree. But, I still think the printS1() of S should be called, because the printS1() of S 'hides' the printS1() of R.
Furthermore, private methods are bound at compile time anyway.
Any insight will be greatly appreciated.
[ August 20, 2002: Message edited by: Rodge Thomas ]