Overloading using an argument that was returned by a method with a common return type

I have a Java overload problem 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 type undefined, so erasing should result in an object type. However, calling f (dummy ()) actually calls f (Collection), not f (Object). Why is this so?

+4
source share

Source: https://habr.com/ru/post/1678392/


All Articles