Generics
On this page 8 topics
Overview
Topics
- Generic classes
- methods
- type parameters
- wildcards basics
Learn first
60 min of reading · 3 h of practice (estimate)
Learning outcomes
- Explain why generics exist (type safety without casts).
- Write a generic class (
Box<T>,Pair<A, B>) and a generic method. - Read signatures with bounded types and wildcards (
<T extends Comparable<T>>,List<? extends Number>).
Lessons
Lab and exercises
Lab
BEG-031-LAB1 · Generic repository
Outcomes: 1–3 · Time: ~2 h Write InMemoryRepository<T, ID> with save, findById, findAll, delete. Use it for two different types (for example Contact and Event). Show one line that fails to compile when the wrong type is saved. Acceptance: no casts; compile-time type safety demonstrated.
Exercises (assessment: four exercises)
| ID | Outcome | Task |
|---|---|---|
| BEG-031-EX01 | 1 | Explain in two sentences what goes wrong with a raw List holding mixed types, and how List<String> fixes it. |
| BEG-031-EX02 | 2 | Write Pair<A, B> with a constructor and getters; create Pair<String, Integer>. |
| BEG-031-EX03 | 2, 3 | Write static <T extends Comparable<T>> T max(List<T> list). |
| BEG-031-EX04 | 3 | Write static double sum(List<? extends Number> nums) and call it with List<Integer> and List<Double>. |
Show solutions
- EX01 A raw list accepts anything, so a bad element is found only when a cast fails at runtime.
List<String>makes the compiler reject non-strings and removes the cast. - EX02
class Pair<A,B>{ private final A first; private final B second; Pair(A a,B b){first=a;second=b;} A getFirst(){return first;} B getSecond(){return second;} } - EX03 Loop keeping
best; replace it whenitem.compareTo(best) > 0. Returnbest. - EX04
double total = 0; for (Number n : nums) total += n.doubleValue(); return total; - The common mistakes (raw types,
new T()) are covered in demo L06; the lab covers outcomes 1–2 as well.
Quiz
Outcomes: O1 why generics · O2 generic classes and methods · O3 bounds and wildcards.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Four exercises BEG-031-EX01 to EX04 plus the lab. Pass: all four exercises correct and the repository compiles without casts or raw types.
Common mistakes
Raw types; trying new T().
Assessment
4 exercises.
Career relevance
Spring Data's JpaRepository<T, ID> (JNR-029) is exactly this pattern.