This code:
List<? extends Reader> weirdList;
weirdList.add(new BufferedReader(null));
has a compile error of
The method add(capture#1-of ? extends Reader) in the type List is not applicable for the arguments (BufferedReader)
Why? BufferedReader extends reader, so why isn't that a "match"?
List<? extends Reader> weirdList
can hold reference to any type of List
that stores any type of Reader
. So it is possible that
List<? extends Reader> weirdList1 = new ArrayList<BufferedReader>();
List<? extends Reader> weirdList2 = new ArrayList<FileReader>();
If Java would allow you to add BufferedReader
to weirdList1
it would also have to let you add BufferedReader
to weirdList2
(reference type is the same) which is not suppose to happen since weirdList2
should store only FileReader
s.
When the compiler sees <? extends Reader>
, then it assumes that it could be any type that is or extends Reader
. It could be something that is imcompatible with BufferedReader
, such as StringReader
. So if the generic type in the class definition shows up in the parameter of a method such as add
, and the type of the instance has something like <? extends Something>
, the compiler must disallow it for type safety reasons. In this example, it could be List<StringReader>
, so you shouldn't be able to add a BufferedReader
here.
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