I get "Type mismatch: cannot convert from List<CherryCoke>
to List<Coke<?>>
"
It looks like a 'list of cherry cokes' is not a 'list of cokes'. This is counterintuitive.
How can I create that 'xs' anyway, if it has to be a List<Coke<?>>
and I have to have a subclass of Coke<Cherry>
?
class Taste { }
class Cherry extends Taste { }
abstract class Coke<T extends Taste> { }
class CherryCoke extends Coke<Cherry> { }
class x {
void drink() {
List<Coke<?>> xs = Arrays.asList(new CherryCoke());
}
}
You're right - a 'list of cokes' is not a 'list of cherry cokes' - a list of 'things that extend coke' is a 'list of cherry cokes' though.
You probably want to define xs
as List <? extends Coke<?>> xs = Arrays.asList(new CherryCoke());
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