Java uses double-dispatch -- the compiler chooses among overloaded methods based on parameter types, and later, at runtime, the JVM chooses among implementations (overridings) of a method based on the runtime type of this
.
You're supplying multiple overloadings, but Java will not automatically switch between them. It looks for an overloading that can accept (Object element)
and finds none so the compiler rejects your program.
One way to fix this is to have a runtime dispatch method:
public void Fun(Object element) {
if (element instanceof Integer) { Fun((Integer) element); }
...
}
This works because its static type is very general, and it calls Fun
with a parameter type whose type is more specific so is dispatched to a different overloading.
Frequent use of instanceof
is often considered an anti-pattern.