Method overloading not working with different parameters

The urban myth answer is:

Because type erasure causes the generics information to be lost. At runtime, those methods appear identical.

Map<X, Y> becomes just Map.

However, the actual answer appears to be more complex. See this excellent answer from a duplicate question. The compiler is actually quite capable of selecting the correct overloaded method given the supplied arguments, however the requirement to support legacy non-generics-aware code has forced the javac developers to forbid it.


This is because of Type Erasure. Type Erasure removes most of the generics information at compile time. So above code after compilation would be

public Object myMethod(Map values) {
   return this;
}

public Object myMethod(Map values) {
   return this;
}

So both the methods are identical at runtime.