Generics Flashcards
Why use generics?
To not limit the use of classes/methods to only one type.
At which level can we use generics?
- class-level generic
- method-level generic
What is an inner class and why to use one?
A class declared inside another class for:
- security (private classes)
- high-density modules (one class frameworks, can be esier to maintain for small projects)
What are the types of inner classes?
- nested inner classes
(idealy private) - method-local inner class (cannot be private/protected/public/static)
- anonymous inner classes(extending the inner class or implementing an interface in-place)
How does class-level generics look like?
class MyCLass <T, U> {
private T t;
private U u;
…}
- this class has generic fields
How does method-level generics look like?
public <T> void method(arguments 'can also be T')
- this method has generic arguments</T>
How does a nested inner class look like?
class A{
public void useInnerA() {…\can use methods from innerA
}
(private) class InnerA {…}
}
How does a method-local inner class look like?
class A{
public void doSth() {
class LocalInnerA {…}
} }
How does an anonymous inner class look like?
class AnonymousA {…}
class A {…}
How does type erasure work in generics?
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>
What does PECS refer to?
Aka. the get-put principle, it is a java idiom to manipulate variance while preserving types.