This may be a dumb question so please excuse my ignorance.
Lets say I have a class:
public class Foo<T extends Base> implements Bar<T> {
private Bar<T> wrapped;
public void setWrapped(Bar<T> input) {
wrapped = input;
}
}
If I call it with:
//Lets just assume methods getNewVal and getFoo exist
Bar<? extends Base> val = getNewVal();
Foo<? extends Base> foo = getFoo();
foo.setWrapped(val);
The compiler says foo.execute(val) is an error. With a message along the lines of The method setWrapped(Bar<capture#20-of ? extends Base>) in the type Foo<capture#20-of ? extends Base> is not applicable for the arguments (Bar<capture#22-of ? extends Base>).
If I try to change Foo to be
public class Foo<T extends Base> implements Bar<T> {
private Bar<T> wrapped;
public void setWrapped(Bar<? extends Base> input) {
wrapped = input;
}
}
The call to foo.setWrapped(val) no longer errors. Instead wrapped = input is an error with a message along the lines of Type mismatch: cannot convert from Bar<capture#1-of ? extends Base> to Bar<T>.
What am I doing wrong? Is there not a way to get the compiler to be okay with a call like this without casting?