1. The Hook (The "Byte-Sized" Intro)
- In a Nutshell: Generic classes define type parameters (<T>) that can be specified when creating instances. Instead of creating StringBox, IntegerBox, PersonBox, you create one Box<T> class.
- Common examples: ArrayList<E>, HashMap<K,V>.
- Naming conventions: T (Type), E (Element), K (Key), V (Value), N (Number).
Think of universal phone case. Instead of manufacturing cases for iPhone 13, iPhone 14, iPhone 15 separately, you create one "Phone" design that adapts to any phone model!
2. Conceptual Clarity (The "Simple" Tier)
💡 The Analogy: The Template Blueprint
Generic class = architectural blueprint with variable room sizes. Customize during construction!
3. Technical Mastery (The "Deep Dive")
Creating a Generic Class
java
public class Box<T> { // <T> is type parameter
private T content; // T used as type
public void set(T content) {
this.content = content;
}
public T get() {
return content;
}
}
// Usage
Box<String> stringBox = new Box<>(); // T = String
stringBox.set("Hello");
String s = stringBox.get(); // No cast!
Box<Integer> intBox = new Box<>(); // T = Integer
intBox.set(123);Multiple Type Parameters
java
public class Pair<K, V> { // Two parameters
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() { return key; }
public V getValue() { return value; }
}
// Usage
Pair<String, Integer> pair = new Pair<>("Age", 25);4. Interactive & Applied Code
java
import java.util.*;
// Generic Box class
class Box<T> {
private T content;
public void set(T content) {
this.content = content;
}
public T get() {
return content;
}
public boolean isEmpty() {
return content == null;
}
}
// Generic Pair class (Multiple parameters)
class Pair<K, V> {
private final K key;
private final V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() { return key; }
public V getValue() { return value; }
@Override
public String toString() {
return key + "=" + value;
}
}
public class GenericClassDemo {
public static void main(String[] args) {
// Box with different types
Box<String> stringBox = new Box<>();
stringBox.set("Hello Generics");
System.out.println("String box: " + stringBox.get());
Box<Integer> intBox = new Box<>();
intBox.set(42);
System.out.println("Integer box: " + intBox.get());
Box<List<String>> listBox = new Box<>();
listBox.set(Arrays.asList("A", "B", "C"));
System.out.println("List box: " + listBox.get());
// Pair examples
Pair<String, Integer> age = new Pair<>("Alice", 25);
System.out.println("Age pair: " + age);
Pair<Integer, String> reversed = new Pair<>(1, "First");
System.out.println("Reversed: " + reversed);
// Nested generics
Pair<String, List<Integer>> scores = new Pair<>(
"Math",
Arrays.asList(90, 85, 95)
);
System.out.println("Scores: " + scores);
}
}5. The Comparison & Decision Layer
Type Parameter Naming Conventions
| Convention | Usage | Example |
|---|---|---|
| T | Generic Type | Box<T>, Container<T> |
| E | Element | List<E>, Set<E> |
| K, V | Key, Value | Map<K, V> |
| N | Number | Calculator<N extends Number> |
| S, U, V | Multiple types | Tuple<S, U, V> |
6. The "Interview Corner" (The Edge)
The "Killer" Interview Question:
"Can you create a static variable of type T in a generic class?"
Answer: NO! Static members belong to the class, not instances. Type parameter T is determined per instance:
java
public class Box<T> {
// ❌ INVALID: Cannot create static of generic type
// private static T defaultValue;
private T content; // ✅ OK (instance variable)
// ✅ OK: Generic STATIC METHOD (different T)
public static <T> Box<T> create(T content) {
Box<T> box = new Box<>();
box.set(content);
return box;
}
}Static generic methods are OK because they declare their own <T>!
Pro-Tip: Diamond operator (Java 7+):
java
// ❌ OLD: Verbose
Box<String> box = new Box<String>();
// ✅ NEW: Type inferred from left side
Box<String> box = new Box<>();