I am using a class Foo
that provides these methods:
String overloadedMethod(Object)
String overloadedMethod(Goo)
Since Java statically dispatches on the non-receiver argument, I cannot just pass my value
(which is an Object
, but might have dynamic type Goo
) and rely on the JVM to dynamically choose the "correct" method.
This is my current (ugly) work-around:
Object value = ...;
Foo foo = new Foo();
if (value instanceof Goo) {
Goo gooValue = (Goo) value;
return foo.overloadedMethod(gooValue); // -> overloadedMethod(Goo)
} else {
return foo.overloadedMethod(value); // -> overloadedMethod(Object)
}
Is there a better way of doing this without modifying the code in Foo
(the class containing the overloaded method)?