CHAPTER 21
Beginner
Generics in Java
Updated: May 17, 2026
5 min read
# CHAPTER 21
Generics in Java
1. Introduction
Before Generics, Collections storedObject types, requiring unsafe casting. Generics add compile-time type safety, ensuring you put the right type in and get the right type out — eliminating ClassCastException at runtime.
2. Generic Classes
java
3. Generic Methods
java
4. Bounded Type Parameters
java
5. Wildcards
-
?— Unknown type
-
? extends T— Upper bound (read-only)
-
? super T— Lower bound (write-only)
java
6. MCQ Quiz with Answers
Question 1
What problem do Generics solve?
Question 2
<T> is called a:
Question 3
<T extends Number> means:
Question 4
Wildcard ? represents:
Question 5
Can primitive types be used with Generics?
Question 6
List<? super Integer> allows adding:
Question 7
Type erasure means:
Question 8
Box<String> and Box<Integer> at runtime are:
Question 9
PECS stands for:
Question 10
Generic methods can be:
7. Summary
Generics provide compile-time type safety, eliminating unsafe casts. Generic classes and methods use type parameters (<T>). Bounded types restrict what types can be used. Wildcards enable flexible method signatures. Type erasure removes generic info at runtime for backward compatibility.