java - What type returns the get method? -
if type variable wildcard, return type of method be?
public class myclass{ } list<? extends myclass> nms = new arraylist<myclass>(); nms.add(new myclass()); //compilation error myclass n = nms.get(0); //compiles fine
the thing return type of get
not pure myclass
type. eclipse's telling me it's cap#2
, argument of add method cap#1
cap#2
subtypeof myclass
, myclass
not subtypeof cap#1
?
so, i'm interested compiler captured type in class hierarchy? if create
public class mysubclass extends myclass{ }
will cap#2
subtype of mysubclass
either?
no, <? extends myclass>
unknown class extends myclass
(can myclass
itself). there no guarantee has relationship mysubclass
.
replace myclass
number
, mysubclass
bigdecimal
. have <? extends number>
. long
. long
has nothing bigdecimal
(even though both extend number
).
as 2 methods, get
return myclass
. add
accept instances of unknown type (and since compiler cannot enforce unknown types, won't let add except null
).
Comments
Post a Comment