Having the following example:
public class Test {
public static class A {}
public static void main(String[] args) {
A a = new A();
m1(a);
}
public static <T> void m1(T t) {
m2(t);
}
public static void m2(A a) {
System.out.println("A");
}
public static void m2(Object o) {
System.out.println("O");
}
}
I do not understand why it is m2(A a)chosen instead m2(Object o). As you can see, when called m2(t), tis A.
Output
How can I use generics for the situation above to choose m2(A a)?
Edit:
I would like to have a general solution that will work even if I add a type B(similar to A).
...
public static void main(String[] args) {
A a = new A();
m1(a);
B b = new B();
m1(b);
}
...
public static void m2(B b) {
System.out.println("B");
}
...
Output
source
share