The following code does not compile, however, if I change f(object) to f(String) or f(Integer) it compiles. I've read the other posts about the subject, but I still don't get it, how come the compiler doesn't know which method to use (in case of a new instance A a = new B();)
public class SampleTester {
public static class A {
public void f(int x) { System.out.print("1"); }
public void f(Object x) { System.out.print("2"); }
}
public static class B extends A {
public <T> void f(T x) { System.out.print("3"); } //compiler error
}
public static void main(String[] args) {
A a = new A();
B b= new B();
a.f(3);
a.f("foo");
b.f(3);
b.f("foo");
}
}
If I change T x
to Object t
it still doesn't compile, so what's the difference? and besides, why it doesn't just override the function from A? (both has the same signature after type erasure