Dear
class,
It
seems I've managed to mess up another example. In class on Thursday (4/17),
we made the following change to B's h method: Instead of h taking a
B argument, we changed it to take an Object argument. While this is valid
and classes A and B and C will compile, the call x.h(w) is now ambiguous.
The ambiguity arises when a subclass overloads a method with a "more
lenient" argument. In this case, we overloaded h with the more
lenient Object. (More lenient means Object is higher on the branch
than A). To the compiler, both A's h(A) and B's h(Object) match. The
compiler cannot tell which one to run. Do not worry; I will not have a
trace problem with ambiguities on the exam. Again, you can assume on the
exam that the entire trace problem compiles. Overloaded methods on
the exam will only have "more strict" arguments, never "more
lenient" arguments. If you stick to the algorithm we discussed in
class, you'll be fine.
class
A {
public void f(A o) { System.out.print("A-f"); }
public void h(A o) { System.out.print("A-h"); }
public void m() { System.out.println(" A-m"); }
}
class
B extends A {
public void f(A o) { System.out.print("B-f"); }
public void h(Object o) { System.out.print("B-h");
} // altered method
public void m() { System.out.println(" B-m"); }
}
class
C extends B {
public void h(C o) { System.out.print("C-h"); }
}
public
class Untitled2 {
public static void main(String[] args) {
A w = new A();
B x = new B();
A y = x;
C z = new C();
x.h(w);
// ambiguous call
}
}
Note
that this ambiguity does not occur when a subclass overloads with a more strict
argument. (i.e. try doing the call x.h(z) on the original problem (when
B's h took a B argument). The ambiguity also does not occur when you
overload in the same class. (Try putting an h(Object) in the A class...no
errrors).
For
those of you not in class, the algorithm I'm talking about is here:
http://www.cs.ucf.edu/courses/cop3330/spr2003/methods.html
Follow this algorithm on the final exam.
While
I've got your attention, THE FINAL EXAM IS FRIDAY APRIL 25 AT 7PM IN OUR
REGULAR CLASSROOM.
Feel
free to email me about this issue. I'll will be available by
appointment up until Friday.
Mat