Java Generics wildcard extends final class -
why java doesn't throw warning when compiling testgenerics
class
, considering string
class
final
, cannot extended?
import java.util.*; public class testgenerics { public void addstrings(list<? extends string> list) { // code here } } }
let's had method this:
public list<? extends t> filteroutnulls(list<t> input) { ...
granted, not best signature in world, still legal. happen if passed list<string>
method? according signature, returns list<? extends string>
. if java disallowed type, it'd impossible use method list<string>
(or @ least, it'd impossible use return value).
secondarily, extends
syntax still useful in case, since list<string>
, list<? extends string>
have different restrictions -- specifically, can't add null
literal list<? extends string>
. i'll use ? extends
signify collection read-only (since t
s can pass in null
), , ? super
signify write-only (since can out t
s object
). isn't fool-proof (you can still call remove methods, pass in null
s, downcast, etc) serves gentle reminder of how collection meant used.
Comments
Post a Comment