Age-old problem: you have two methods whose signatures only differ in one argument, and in one method the type of that argument is a super-class of the type of the same argument in the other method.
e.g.
public void method1( String a ) {} // 1
public void method1( MyExtendedString a ) {} // 2
}
...
class MyExtendedString extends String
{}
If you invoke:
MyExtendedString sub = new MyExtendedString( "blah" );
String parent = sub;
method1( parent ); // A
method1( sub ); // B
method1( (String) sub ); // C
you have the joy of not being entirely sure which method is going to get called. Obviously, A will be executed by 1, and B will be executed by 2.
The way it works is that C will then be executed by 1 (nb: if you like, I’ve got code that will demonstrate this happening).
My implementation of method 1 above checks if the argument is an instance of the subclass, and if so casts it to the subclass, and calls the same method again which, I would expect to cause method 2 instead to be invoked on the subclass - but it doesn’t, it just calls itself again (JDK 1.4.2).
Is this right? You can cast a sub class to a super to change the function that’s despatched, but you can’t do vice versa? But if you pass in a valid sub class it WILL be despatched to the subclass-specific function? I regularly (if infrequently) get problems with differences between how I remember java’s function despatcher and how it actually is, but I thought I had it pretty well sussed by now :(.