데이터 타입(data type)을 일반화(generalize)하는 것
클래스나 메소드에서 사용할 내부 데이터 타입을 컴파일 시에 미리 지정하여
컴파일 시에 미리 타입 검사(Type Check)를 수행
class Apple {}
class Banana {}
class fruitBox {
private Object fruit;
public FruitBox(Object fruit) { this.fruit = fruit; }
public Object getFruit() { return fruit; }
}
public static void main(String[] args) {
FruitBox appleBox = new FruitBox(new Apple());
FruitBox bananaBox = new FruitBox(new Banana());
Apple apple = (Apple) appleBox.getFruit();
// Banana banana = (Banana) appleBox.getFruit(); // 런타임 에러 발생
}
class Apple {}
class Banana {}
class fruitBox<T> {
private T fruit;
public FruitBox(T fruit) { this.fruit = fruit; }
public T getFruit() { return fruit; }
}
public static void main(String[] args) {
FruitBox<Apple> appleBox = new FruitBox(new Apple());
FruitBox<Banana> bananaBox = new FruitBox(new Banana());
Apple apple = appleBox.getFruit();
// Banana banana = appleBox.getFruit(); // 컴파일 에러 발생
}

class fruitBox<T> {
private List<Fruit> fruits = new ArrayList<>();
public void add(T fruit) { fruits.add(fruit); }
}
FruitBox<T>FruitBox<T extends Fruit>FruitBox<T super Fruit>Reference