Generic Class
A Generic Class allows the definition of classes with a placeholder for a type, enabling reusability and type safety. The type is specified when the object is instantiated.
Syntax
class Box<T> {
T item;
void set(T item) { this.item = item; }
T get() { return item; }
}
Usage
public class Main {
public static void main(String[] args) {
Box<Integer> intBox = new Box<>();
intBox.set(100);
System.out.println("Integer Value: " + intBox.get());
Box<String> strBox = new Box<>();
strBox.set("Hello Generics");
System.out.println("String Value: " + strBox.get());
}
}
Multiple Type Parameters
class Pair<K, V> {
K key;
V value;
Pair(K key, V value) {
this.key = key;
this.value = value;
}
K getKey() { return key; }
V getValue() { return value; }
}
Benefits
- Reduces code duplication
- Type-safe data structures
- Easier to debug and maintain