Originally posted by Michal Wiezik:
You have two classes, one inheriting from another, both with two methods, parameterless and with parameters:
class A {
void m1(A a) { System.out.println("A.m1(A a)"); }
void m2() { System.out.println("A.m1()"); }
}
class B extends A {
void m1(B b) { System.out.println("B.m1(B b)"); }
void m2() { System.out.println("B.m1()");
}
}
The result of:
A b1 = new B();
B b2 = new B();
b1.m1(b2);
b1.m2();
is:
A.m1(A a)
B.m1()
Why does the absence of parameter has an influence on choosing the method's class?
Thnx a lot.
I believe the first method is overloaded, not overriden. In the case of overloaded methods, the method to call is determined at compile time. Since the reference variable is of Class A, it will call Class A's version of m1. If the method were overriden, it would have called Class B's version
[ July 23, 2004: Message edited by: Chris Allen ]