#10 generics + wild card Flashcards

1
Q

define a generic class

A
class MyGeneric
(any capital letter will do)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

instantiate something of a generic class type

A

ArrayList x = new ArrayList<>;

MyGeneric V;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what types can be used in generic

A

reference types and wrapper

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

before generics

A

objects had to be downcast when returned from library classes?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

can an arraylist of super class type hold subclass and superclass objects?

A

yes!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

bounded type parameter

A
class MyGeneric
(only vehicle or vehicle subclass types can be passed in)
(vehicle methods can be called from E references)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

if S is a subclass of T, is Generic a subclass of Generic ?

A

no, only if you add

Generic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

wild card parameter >

A

any unknown type!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

ArrayList extends Vehicle>

A

arraylist of any type Vehicle or subclass of vehicle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

ArrayList super Vehicle>

A

arraylist of any type Vehicle or superclass of vehicle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

when can wild card be used?

A
  1. reference declaration
  2. some method return vales
  3. CANNOT instantiate objs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

element type of array cannot be type param of a generic class. what is the workaround?

A

array of object references and cast it to the generic type

causes unchecked cast so write
@SuppressWarnings(“unchecked”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly