Generics Flashcards

1
Q

What is Type erasure?

A

Process of converting generics to Objects

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

Static and generics?

A

Not permitted. static T

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

Primitives and generics?

A

Not permitted. < int >

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

Instantiating a generic?

A

Not permitted. new T()

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

Checking instanceof a generic?

A

Not permitted.

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

Using generic in ArrayList?

A

Not permitted. new ArrayList< T >

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

What generics actually become after compile?

A

Object

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

What types of wildcard generics do we have?

A

Unbounded, upper bounded, lower bounded

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

What are unbound generics?

A

<?>

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

Can we pass multiple generics?

A

Yes. <T, U>

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

Can wildcard generics be used as return types?

A

No

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

Where do we use wildcard generics?

A

Mostly as parameters or as local variable types

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

Why would we use wildcard generics?

A

To define code that can be more universal

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

At what level are generics resolved?

A

At instance level

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

Why cannot we use generics with static?

A

Because static is resolved at class level, not instance level like generics

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

Where can static be used with generics?

A

On static methods with < T >

17
Q

What does “public static < T >T identity(T t)” mean?

A

It is a method of name “identity”, where we use type parameter < T >, which defines return type T and method parameter T.

18
Q

What does “public static T noGood(T t)” mean?

A

No good. Compile error, since we dont tell compiler that we want to use generic type. We define object T as return type, and object T as parameter type.

19
Q

What does “public static < T > void sink(T t)” mean?

A

Method that has no return type, but we define type parameter < T > for method parameter T t

20
Q

Where can T be used at?

A

As member types, as parameter argument types, as method parameter types

21
Q

In what types of classes can generics be used?

A

In classes, records, interfaces

22
Q

Can wildcard generics be used with generic types?

A

Yes. <? extends T>

23
Q

Can we use upper and lower bounds on generic types?

A

Yes. < T extends SomeClass >

24
Q

Where can we use upper and lower bound generic types?

A

At same places (reference types) where we can use generic types

25
Q

When to use “<? extends T>”

A

When we want to make collection read only

26
Q

When to use <? super T> (lower bound wildcard)

A

When we want to make collection write only

27
Q

What should we watch out regarding generics and method parameters?

A

Watch for overloading problems - eg. List< T > becomes List< Object >, so we cannot have two methods with those in same signature

28
Q

What should we watch out regarding generics and return types when overriding?

A

Return type must be covariant, so watch out when overriding methods with generics

29
Q

Why “lower bound wildcard” as name, or “upper bound wildcard”?

A

Wildcard can be any class.
Bound means depicts that something linked or depends on something.
Regarding lower or upper, imagine class diagrams.

Lower bound, or super, is drawn from down to up in hierarchy.

Upper bound, or extends, is drawn from up to down.

30
Q

Can wildcards be used when instantianting objects?

A

No, Java requires to be type specific. References can use wildcards tho.