I have the following code:
public <T extends SomeObject> long doSomething(T someObject){
List<? extends SomeObject> l = new LinkedList<>();
l.add(someObject);
}
this causes a compilation error – telling me that there is no suitable methods found: add(T),
why is that?
If l accept things that extends SomeObject shouldn’t it accept someObject as it bounds to extend SomeObject?
What do you mean by that? Of course it will generate an error.
Take this example :
SomeObjectisFruit, you have 2 derived classesAppleandOrangeYour list what will it contain?
Apples orOranges? The compiler cannot tell. So it generates error.If you replace
List<? extends SomeObject> lwithList<SomeObject> l. Then this will work becauseAppleandOrangeare bothFruit.