java - Generic class references -
java - Generic class references -
this question has reply here:
is list<dog> subclass of list<animal>? why aren't java's generics implicitly polymorphic? 11 answersplease next code. why error thrown though integer inherited number , why same case of error not there when '? extends number' used.
public class testgeneric { public static void main(string[] args) { class<integer> classint = int.class; class<number> classnum1 = int.class;// error type mismatch: cannot convert // class<integer> class<number> class<? extends number> classnum2 = int.class; } }
you not have co-variance (if word looking for) in generic types.
they need match exactly.
class<number>
can assigned number.class
.
if want allow subclassed (or superclasses) have utilize extends
or super
.
class<? extends number>
can take integer.class
(among others, such long.class
or number.class
).
class<? super number>
can take number.class
or object.class
.
this different methods of generic type beingness able work on subclass instances.
for example, can add together integer
list<number>
, not create list<number>
assignable list<integer>
(or vice versa).
an object instance can used superclass required, generic type cannot.
java
Comments
Post a Comment