4

I have a Java overloading issue that I cannot understand.

The code is as follows:

private void f(Object o) {
}

private void f(Collection c) {
}

private <T> T dummy() {
    return null;
}

private void g() {
    f(dummy());
}

Now, dummy() returns an object of an undefined type so erasure should lead to a type of Object. However, the invocation f(dummy()) actually calls f(Collection) and not f(Object). Why is this the case?

Erik
  • 91
  • 1
  • 4
  • 1
    I think since `T` can be anything and the compiler always chooses the most *specific* method for overloading, the one with `Collection` is chosen. Because `Object > Collection` – QBrute Jun 02 '17 at 08:46
  • 1
    It is a good question, but it was already discussed – Andremoniy Jun 02 '17 at 08:51
  • thanks for your response. Could not quickly find the answer to it but now I know. – Erik Jun 02 '17 at 08:56

0 Answers0