<aside> 💡 객체 지향 프로그래밍 입문 시리즈
인프런의 객채 지향 프로그래밍 입문을 기반으로 작성한 게시물 입니다. (본 시리즈의 모든 예시는 JavaScript(TypeScript)로 변환하여 작성하였습니다.)
</aside>
"다형성과 추상화"
한 객체가 여러 타입을 갖는 것
예)
public class Timer {
public start(): void {}
public stop(): void {}
}
public interface Rechargerble() {
charge(): void;
}
public class IotTimer extends Timer implements Rechargeable {
public charge(): void {
...
}
}
const it: IotTimer = new IotTimer();
it.start();
it.stop();
const t:Timer = it;
t.start();
t.stop();
const r: Rechargeable = it;
r.charge();
IotTimer
는 Timer
클래스와 Rechargeable
인터페이스를 상속하고 있기 때문에 Timer
클래스와 Rechargeable
모두에 할당이 가능하다.
그렇다면 다형성을 왜 추구하는 것일까.
이는 추상화 때문이다.
추상화란 데이터나 프로세스 등을 의미가 비슷한 개념이나 의미 있는 표현으로 정의하는 과정이다.
추상화는 두가지 방식이 있다.