Generics Flashcards

1
Q

Why use generics?

A

To not limit the use of classes/methods to only one type.

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

At which level can we use generics?

A
  1. class-level generic
  2. method-level generic
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an inner class and why to use one?

A

A class declared inside another class for:
- security (private classes)
- high-density modules (one class frameworks, can be esier to maintain for small projects)

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

What are the types of inner classes?

A
  1. nested inner classes
    (idealy private)
  2. method-local inner class (cannot be private/protected/public/static)
  3. anonymous inner classes(extending the inner class or implementing an interface in-place)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does class-level generics look like?

A

class MyCLass <T, U> {
private T t;
private U u;
…}
- this class has generic fields

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

How does method-level generics look like?

A

public <T> void method(arguments 'can also be T')
- this method has generic arguments</T>

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

How does a nested inner class look like?

A

class A{
public void useInnerA() {…\can use methods from innerA
}
(private) class InnerA {…}
}

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

How does a method-local inner class look like?

A

class A{
public void doSth() {
class LocalInnerA {…}
} }

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

How does an anonymous inner class look like?

A

class AnonymousA {…}
class A {…}

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

How does type erasure work in generics?

A

The Java compiler replaces the “special parameter” with real types. That means List<Integer>, List<String> and List<List<String>> have the same type at run-time.</String></String></Integer>

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

What does PECS refer to?

A

Aka. the get-put principle, it is a java idiom to manipulate variance while preserving types.

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