List<B>
does not extend List<A>
. There are good reasons for that; informally, consider that the contract for List<A>
"you can put an instance of A in it" is not valid for List<B>
.
You need to indicate that you are willing to accept lists with subtypes of A
. You can do that with a wildcard like List<? extends A>
. However then your return type also becomes List<? extends A>
and you don't want that. What you really want is that if you pass in List<B>
, the return type is List<B>
.
To do this properly without casts, you need to bind the type and specify it can sublass A.
public static <T extends A> List<T> doSomething(List<T> items, T item) {
// manipulate items
return items;
}
This should work.