1) 여러가지 형태를 가질 수 있는 능력
2) 조상 타입 참조변수로 자손 타입 객체를 다루는 것
class SmartTv extends TV {
....
}
smartTv가 Tv부모객체를 상속받은 형태일때,
Tv t = new SmartTV();
와 같이 사용할 수 있는 것.
SmartTv s = new SmartTv(); //참조변수와 인스턴스 타입이 일치
Tv t = new SmartTv(); // 조상참조변수로 자손인스턴스 참조
SmartTv s = new Tv(); // 이건 에러. * 왼쪽이 리모콘이라고 생각하면 이해하기 쉬움
사용할 수 있는 멤버의 갯수를 조절하는 것
class FireEngine extends Car {
}
FireEngine f = new FireEngine();
Car c = (Car) f
FireEngine f = (FireEngine) c // 형변환 실행에러 java.lang.classCastException
형변환 전에 instanceof 연산자로 형변환 가능여부를 반드시 확인하고 변환해야 함
}
//c가 fireEngine인지, c가 FireEngine의 child or 자기자신인지를 확인하는 것
if (c instance of FireEngine) {
}
System.out.println(fe instanceof Object); //true
System.out.println(fe instanceof Car); // true
System.out.println(fe instanceof FireEngine); // true
1) 다형적 매개변수: Polymorphism allows objects to be treated as instances of their parent class
Product p = new TV();
Product c = new Computer();
class buy(Tv t) {
}
class buy(Computer c) {
}
// 이렇게 다 따로 오버로딩 해주는게 아니라
class buy(Product p) {
}
// 이렇게 하나로 써줄 수 있음
2) 여러종류의 객체를 배열로 다루기
Product p[] = new Product[3];
p[0] = new Tv();
p[1] = new Computer();
p[2] = new Audio();