Overloading considered harmful - answers

Here are the answers to Tuesday's quiz on overloading and dynamic method
invocation.  First here are the programs again:

// All access restrictions (public/protected/private) omitted for clarity

class Point{
   void equal(Point x){...}    //*1*

class ColorPoint extends Point{
   void equal(ColorPoint x){...}    //*2*

Point p1 = new Point();
Point p2 = new ColorPoint();
ColorPoint cp = new ColorPoint();

For each method call, say which version of equal is called:

1.	p1.equal(p1);

2.	p1.equal(p2);

3.	p2.equal(p1);

4.	p2.equal(p2)

5.	cp.equal(p1);

6.	cp.equal(p2);

7.	p1.equal(cp);

8.	p2.equal(cp);

9.	cp.equal(cp);

Answers:  All but #9 call the method from Point (*1*).

Since overloading is determined statically, all equal messages sent to p1 or p2
look for methods available in Point, and since only one is available (and
it is not overridden in ColorPoint), that one is called.   Equal messages sent
to cp could be resolved into either *1* or *2* since Java allows overloading
across subclasses.  Since the choice is made statically, and #5 and #6 both
have parameters whose static type is Point, both call "equal(Point)".  Only
#9 statically resolves to *2*.

In C++, #5 and #6 would be illegal since one cannot overload across subclasses.

By the way, if we overrode equal(Point) in ColorPoint (i.e., added an occurrence
of that method with a possibly different body, but same signature), then
#5 and #6 would both call that new method body (in either Java or C++).

While most of you could probably get virtually all with some work, because
I warned you that it would be tricky, imagine how the poor programmer would
deal with this when they weren't expecting a problem.

I would personally prefer to avoid static overloading in OO languages
(though on the plus side it does allow hacks to support parametric polymorphism
a la Pizza or GJ via the homogeous translations).

	Kim Bruce

P.S.  I obviously should have made equal return a boolean in the code above, 
but it makes no difference in making my points.