조상 타입 참조 변수로 자손 타입 객체를 다루는 것
하나가 여러 가지 형태를 가질 수 있는 능력
SmartTv s1 = new SmartTv();
Tv t = new SmartTv(); // 타입 불일치
SmartTv s2 = new Tv(); // ERROR
s1은 자신의 멤버 뿐만아니라 t의 멤버까지 모두 사용 가능하지만, t는 자신의 멤버만 사용 가능하다.
자손 타입의 참조 변수로 조상 타입의 객체를 가리킬 수 없다.
class Product {
int price, bonusPoint;
Product(int price) {
this.price = price;
bonusPoint = (int) (price / 10.0);
}
}
class Tv2 extends Product {
Tv2() {
super(100); // 조상 생성자의 매개변수
}
public String toString() {return "TV";}
}
class Computer extends Product {
public Computer() {
super(200);
}
public String toString() {return "COMPUTER";}
}
class Buyer {
int money = 1000;
int bonusPoint = 0;
// 다형성
// Product p1 = new Tv();
// Product p2 = new Computer();
// Product p3 = new Audio();
void buy(Product p) { // 다형적 매개변수
if (money < p.price) {
System.out.println("잔액부족");
return;
}
money -= p.price;
bonusPoint += p.bonusPoint;
System.out.println(p + " 구매 완료"); // p == p.toString()
}
}
public class c7_9_210414 {
public static void main(String[] args) {
Buyer b = new Buyer();
Product p = new Tv2();
b.buy(p);
// b.buy(new Tv2());
b.buy(new Computer());
System.out.println("남은 돈:" + b.money);
System.out.println("보너스 점수:" + b.bonusPoint);
}
}