I would like to take the contents of the G.myUglyList list here and pass it to the Outer.send() method. I do not understand why this gives a compiler error. ? extends Inner is the type I parameterize Outer with. So why is it rejecting an Inner passed to it? It wants a "? extends Inner", which isn't a type.
I want the list declared as List<Outer<? extends Inner>>
so it can take subtypes of Inner. (Please see the edit below for why this is)
interface Outer<T> {
void send(T message);
}
interface Inner {}
interface Inner2 extends Inner {}
public class G {
List<Outer<? extends Inner>> myUglyList;
void foo() {
Inner xxx = null;
for (Outer<? extends Inner> outer : myUglyList) {
outer.send(xxx); //error
}
}
}
I get this error:
error: method send in interface Outer<T#2> cannot be applied to given types;
required: CAP#1
found: Inner<T#1>
reason: actual argument Inner<T#1> cannot be converted to CAP#1 by method invocation conversion
where T#1,T#2 are type-variables:
T#1 extends Object declared in class G
T#2 extends Object declared in interface Outer
where CAP#1 is a fresh type-variable:
CAP#1 extends Inner<T#1> from capture of ? extends Inner<T#1>
edit: I got a lot of answers saying just make the list of type List<Outer<Inner>>
, but that is incorrect. I will not be able to add subtypes of Inner if I do that. If I try to add an Outer<Inner2>
, it would fail. So list must be of type List<Outer<? extends Inner>>
.
interface Inner2 extends Inner {}
class G {
void foo() {
Outer<Inner2> foiled = null;
myUglyList.add(foiled); //this will fail if list is of type List<Outer<Inner>>
Inner xxx = null;
for (Outer<? extends Inner> outer : myUglyList) {
outer.send(xxx); //error
}