I have an error in some classes when I use generics. I specified where I get the error. I can't get rid of it. I tried with casts, and everything I could think of.
class UltraTable<O extends Object> {
public void setContent(Collection<O> collection) {
}
}
class ObjectA {
}
class AShell {
protected UltraTable<? extends ObjectA> tableA;
protected List<? extends ObjectA> getAObjects() {
return null; // the list is created here
}
}
class BShell extends AShell {
public BShell() {
tableA.setContent(getAObjects()); // THE ERROR IS HERE!
}
}
How can I make it work by changing only in BShell
class, if possible?
The error message I get is:
The method setContent(Collection<capture#1-of ? extends ObjectA>)
in the type UltraTable<capture#1-of ? extends ObjectA>
is not applicable for the arguments (List<capture#2-of ? extends ObjectA>)
Update:
If I change my code as Thomas Jung said, I get errors in other classes at the constructor of tableA
and the method getAObjects()
:
class ObjectX extends ObjectA {}
class XShell extends AShell {
public XShell() {
tableA = new UltraTable<ObjectX>();
tableA.setContent(getAObjects()); // THE SAME ERROR AS ABOVE
}
@Override
protected List<ObjectX> getAObjects() {
return null;
}
}
I often have a UltraTable and getAObjects() returns a list of DerivedClass which extends BaseClass. This should work in all cases.
Anyway, I think that the error message is illogic: "... <capture#1-of ? extends ObjectA>
is not applicable for the arguments (List<capture#2-of ? extends ObjectA>
)"
capture#1 and capture#2 both extend ObjectA! What's wrong here?