Re: which foo(x)

From: Daniel Bonniot (Daniel.Bonniot_at_inria.fr)
Date: 12/17/03


Date: Wed, 17 Dec 2003 20:31:53 +0100
To: Rick Trotter <rht325@yahoo.com>


Hi,

(Sorry for this late answer, I'm not reading the forum regularly)

> I was trying to write some polymorphic application code and found that
> a superclass method implementation gets invoked when I expect a
> subclass implementation. Here's how I have abstracted the problem.

You did a good analysis of the situation of Java. The key you seem to
miss to understand the "logics" behind Java's behaviour is the
following. In Java, a method call is of the form
   x.meth(a);
meth is a method name, and x is call the receiver of the call. There
could be more than one argument a, but it does not matter here.
The way the code to execute for this call is like this:
   1) during compilation, the compiler looks at the (static) type of a,
and looks in the class C which is the (static) type of x a method with
the name "meth" that matches the type of a (not relevant, but for the
sake of completeness: it also consider methods defined in super-classes
of C, and if there are more than one possible method, it will choose the
most specific). Let's call the result of this search method M.
   2) during execution, there is a mechanism to "refine" this search, by
looking at the actual class of the object that x represents. It is sure
that this class is either C, or a subclass of C. If it is a subclass D,
then a method with the same signature as M might have been defined in D,
or somewhere between C and D. In that case, that method will be called,
not M. However, if a method has a more precise signature than M because
it handles only a subclass of the original class for the argument, it is
not considered, even when this more precise method would be applicable
to the runtime argument of the call. It is this last situation that
surprised you in your example.

So you can see that there is a fundamental asymetry in a method call:
the type of the receiver will be checked during execution to find a more
precise method, but not the types of the arguments.

The behaviour of Java, C++, Eiffel and most object-oriented languages is
called "single dispatch" because they dispatch (choose the method to
execute) based on the type of a single argument. A few other OO
languages use "multiple dispatch", because, you guessed it, they use the
type of multiple arguments to decide what method to execute. To name a
few: Cecil, Dylan, CLOS. I am myself the main designer of a language
called Nice, which is a variation on Java that supports multiple
dispatch. For instance, thanks to multiple dispatch, you can write in Nice:

class Point
{
   private int x;

   equals(Point that) {
     return this.x == that.x;
   }
}

while Java programmers need to write:

class Point
{
   private int x;

   public boolean equals(Object that) {
     if (that instanceof Point) {
       Point thatPoint = (Point) that;
       return this.x == thatPoint.x;
     } else {
       return super.equals(that);
     }
   }
}

Both codes are stricly equivalent, but I believe the Nice version is
easier to write and to read ;-)

If you want to find out more, Nice's homepage is http://nice.sf.net

Daniel



Relevant Pages

  • Re: operator overloading
    ... Multipliable, Ring extends Addable, Multipliable, Dividable, Field extends ... There is a class of types, Num, which represent ... little in the Java SE library that I would opt to remove. ... and you subclass BigDecimal as ...
    (comp.lang.java.programmer)
  • Re: IDE crashes on any error
    ... I even subclass quite a bit throughout my app and can push the end button while a subclass is active with no problems. ... The fact is most components will release their resources when the end button is pushed, even components written in vb6 as long as their source code isn't loaded into the IDE. ... Neither the End toolbar button nor the End statement will cause these to be properly released, cleaned up, deallocated, or whatever because specific code in the application must execute to to accomplish that task, and clicking the End toolbar button or using the End statement doesn't allow that code to execute. ...
    (microsoft.public.vb.general.discussion)
  • Re: [ANN] jfli - a Java FLI for LispWorks
    ... Based on what you write there, I managed to run it with the Java SDK - see my ... >> in using Java libraries, the ability to create instances of Java classes and ... > Well, a nice library, such as SWT/Eclipse, often offers interfaces. ... Yes, although even in those cases it is sometimes more convenient to subclass, ...
    (comp.lang.lisp)
  • Re: Can anybody please check this code out?
    ... I'm rather inexperienced in Java but I'm in need of defining a subclass ... public boolean isCellEditable(int rowIndex, int columnIndex) ... so that a cell must be editable according to the superclass, ...
    (comp.lang.java.programmer)
  • Re: Why does super take a class name as the argument?
    ... > superclass method does not require an explicit name of the current ... When an inherited method is augmented in a subclass, ... why can't a call like self.superexecute the code that ... you'd get if it tried to derive who "super" is based on "self"). ...
    (comp.lang.python)