I am trying to write a method that adapts to different types of interfaces using generics.
In this code I would have expected that the method f an g would print out "BB" instead of "interface A". Why did this happen?
interface A {
String n="interface A";
}
interface B extends A {
String n = "BB";
}
public class Main {
<T extends A> void f() {
T a = null;
System.out.println(a.n);
}
<T extends A> void g() {
System.out.println(T.n);
}
public static void main(String[] args) {
Main m = new Main();
m.<B>f();
m.<B>g();
}