One Dan's Mock Exam Question:
class C {
void printS1() {System.out.print("C.printS1 ");}
static void printS2() {System.out.print("C.printS2 ");}
}
class D extends C {
void printS1(){System.out.print("D.printS1 ");}
void printS2() {System.out.print("D.printS2 ");}
public static void main (
String args[]) {
C c = new D();
c.printS1(); c.printS2();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: C.printS1 C.printS2
b. Prints: C.printS1 D.printS2
c. Prints: D.printS1 C.printS2
d. Prints: D.printS1 D.printS2
e. Runtime error
f. Compiler error
g. None of the above
the answer is "f" compiler error: "printS2() in D cannot override printS2() in C;overridden method is static"
if I change "void printS2() {System.out.print("D.printS2 ");}" into "static void printS2() {System.out.print("D.printS2 ");}" in Class D , the file is compiled properly. I got this running results:
"D.printS1 C.printS2"
My question is :
1.If the printS2 method is overriden or it happen to have a same method in Class D.
2.Why I got this output but not D.printS1 D.printS2 or C.printS1 C.printS2?
Thanks in advance!
Sincerely Sam
[ January 03, 2003: Message edited by: sam huang ]