Product p1 = new Tv();
Product p2 = new Computer();
Product p3 = new Audio();
위의 코드를 다음과 같이 바꿀 수 있다.
Product p[] = new Product[3];
p[0] = new Tv();
p[1] = new Computer();
p[2] = new Audio();
예제
class Buyer {
int money = 1000;
int bonusPoint = 0;
// 다형성이 되기 때문에 하나의 배열에 여러 종류의 객체를 담아 관리가 용이해진다.
Product[] cart = new Product[10]; // 구입한 물건을 담을 배열
void buy(Product p) { // 메서드 하나로 여러 물건을 살 수 있다. buy(new XX())
if(money < p.price) {
System.out.println("잔액 부족");
return;
}
money -= p.price;
bonusPoint += p.bonusPoint;
cart[i++] = p; // 카트에 저장
}
}