Why is type inference not working in this particular case?
public class ClassB<O> {
public <T> T echo(T msg) {
return msg;
}
public void doSomething(O a) {
}
public static void main() {
// Test 1
ClassB<Object> sampleB = new ClassB<>();
// No compile time type error.
// <T> and <O> are different.
String test = sampleB.echo("");
// Test 2
ClassB sampleB1 = new ClassB();
// Causes compile time type error.
String test2 = sampleB1.echo("");
}
}
What does method return type <T> has to do with <O>?
Is compiler treating this as
String test2 = (Object) sampleB1.echo("");
instead of
String test2 = (String) sampleB1.echo("");