#10 generics + wild card Flashcards
(12 cards)
define a generic class
class MyGeneric (any capital letter will do)
instantiate something of a generic class type
ArrayList x = new ArrayList<>;
MyGeneric V;
what types can be used in generic
reference types and wrapper
before generics
objects had to be downcast when returned from library classes?
can an arraylist of super class type hold subclass and superclass objects?
yes!
bounded type parameter
class MyGeneric (only vehicle or vehicle subclass types can be passed in) (vehicle methods can be called from E references)
if S is a subclass of T, is Generic a subclass of Generic ?
no, only if you add
Generic
wild card parameter >
any unknown type!
ArrayList extends Vehicle>
arraylist of any type Vehicle or subclass of vehicle
ArrayList super Vehicle>
arraylist of any type Vehicle or superclass of vehicle
when can wild card be used?
- reference declaration
- some method return vales
- CANNOT instantiate objs
element type of array cannot be type param of a generic class. what is the workaround?
array of object references and cast it to the generic type
causes unchecked cast so write
@SuppressWarnings(“unchecked”)