Hi all,
Please refer question 20:
http://www.danchisholm.net/july21/topic/section6/inherit1.html Question 20
class A {void m1() {System.out.print("A");}}
class B extends A {void m1(){System.out.print("B");}}
class C extends B {void m1() {System.out.print("C");}}
class D extends C {
void m1() {System.out.print("D");}
void m2() {
m1();
((C)this).m1(); // 1
((B)this).m1(); // 2
((A)this).m1(); // 3
}
public static void main (
String[] args) {
new D().m2(); // 4
}}
What is the result of attempting to compile and run the program?
a. Prints: DCBA
b. Prints: DDDD
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
g. Run-time error
h. None of the above
Answer to Question 20: = b Prints: DDDD
http://www.danchisholm.net/july21/topic/section6/inherit1ans.html ------------------------------------------------------------------
My question is doesn't "((C)this).m1();" would have cast the D object into a C object? If so, why can't the answer be "DCBA"?
Thank you.