I encountered a strange problem in my project. Now I have simplified the problem and written a small example here to illustrate my confusion:
public class Question {
class Q1 {}
class Q2 extends Q1 {}
interface In<T> {
void f(T t);
}
List<Q2> list;
void f(In<? super List<? super Q2>> in) {
in.f(list);
}
static void g() {
Question question = new Question();
In<Collection<Q1>> in1 = new In<Collection<Q1>>() {
@Override
public void f(Collection<Q1> o) {}
};
In<List<Q2>> in2 = new In<List<Q2>>() {
@Override
public void f(List<Q2> o) {}
};
question.f(in1); //Error!
question.f(in2); //Error!
}
}
My aim is to make the method f(In<? super List<? super Q2>>)
more flexible. I can pass in1
or in2
to the method. But neither can be passed! What is wrong?
Maybe this answer will make some sense. But my question is different!!! My generic type is In<? super List<? super Q2>>
, a generic type within a generic type.