is this possible? if not, why isn't this possible in Java?
interface B extends A {}
public List<B> getList();
List<A> = getList(); // Type mismatch: cannot convert from List<B> to List<A>
I think the topic I'm looking for is "covariant types" as here and here, but its murky and it doesn't solve my problem.
Here is an intuitive example of how this can make things go horribly wrong:
interface B extends A {}
List<B> blist=new List<B>();
List<A> alist=blist;
alist.add(new A()); //should be ok, right?
B b = blist.get(0); //fail: even though blist is a List<B>, it now has an A in it
Try
List<? extends A> = getList()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With