hi in one of Dan quesion in oops chapter
as follow
---------------------------
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
the answer of above is "b"
with explanation as ...
The instance method that is invoked depends on the run-time type of the object--not the compile-time type of the reference. In each case, the method m1 is invoked on an object of type D; so the implementation of m1 in type D is selected each time.
I didn't understand why not "a" option is right ?