class A {}
class B extends A {}
Hi all
java explorers,
the following question appears in Dan's Exam 1 on Operators and Assignments
class C extends B {
static void m1(A x) {System.out.print("m1A");}
static void m2(B x) {System.out.print("m2B"); m1(x);}
static void m2(A x) {System.out.print("m2A"); m1(x);}
static void m3(C x) {System.out.print("m3C"); m2(x);}
static void m3(B x) {System.out.print("m3B"); m2(x);}
static void m3(A x) {System.out.print("m3A"); m2(x);}
public static void main(
String[] args) {
m3(new C());
}
}
ANSWER THEY GAVE IS
Prints: m3Cm2Bm1A
Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, the most specific version of method m3 is the one that declares a parameter of type C. The most specific version of m2 is the one that declares a parameter of type B.
But I can't understand how m2 method is getting called as the parameter passed to it is object of Class c
static void m3(C x) {System.out.print("m3C"); m2(x);}
plz explain to me what is going on here? ?
thanx in advance,
Vishal.