posted 21 years ago
Thanks Dan, it's your tests by the way that are kicking my butt...good or bad, I'm not sure.
Anyway, I think I understand now, but I may disagree with the wording you are using. The way you are explaning it sounds like this. You are saying because the method m1() is overloaded it means the method needs to be declared at compile time. Ok I agree with that (now). But then you say because c2 is a referenced by an reference of type A, and since "A" doesn't have scope to see the other overriden m1() methods, then the lines that read:
c2.m1(a1);
c2.m1(b1);
c2.m1(c1);
will have the parameter automatically cast up to type A.
Now this may be slight. How I see it, and from what others are telling me, is that c2 is referenced by a reference of type "A." And because the methods are overloaded then it needs to be determined which method exactly it should be using at compile time. This is where we differ. I think the compiler then says "ok c2 is referenced by a type "A", therefore that is what it is. Now go to the object A and see if there is an m1() method, and see if it takes a parameter that matches." See the difference in what I am saying and what you are saying is that I think teh compiler cares more about the type of reference that is pointing to the object that the object type of the parameters. I think the compiler, once it has determined that the object in question is an "A" it will only go to the A.m1() regardless of what parameter is in the method call (as long as it works). What you are saying is that the compiler changes the parameter so that it matches the parameter of A's m1().
I'm not sure if there is even a difference in what we are saying...but it's just how I understand it I guess. What do you think?
P.S. If I changed the B object to this:
class B extends A {
void m1(B b) {
System.out.println("B");
}
void m1(C c){
System.out.println("C");
}
}
and ran the main again, it will still output AAA...