Abstract Class
- 보통 OOP 에서 추상 클래스는 클래스의 형태만 제공하고 실제 인스턴스화는 불가능한 클래스이다.
- 상속하여 인스턴스화 가능한 클래스를 만들 때 사용한다.
일반 사용법
abstract class AbstractClass {
id: number;
constructor(id: number) {
this.id = id;
}
}
// 인스턴스화를 시도할 시에 에러가 난다.
const abstract = new AbstractClass(123);
// 보통 이렇게 상속을 하여 쓴다.
class Class extends AbstractClass {}
// 당연히 상속한 클래스는 인스턴스화가 가능하다.
const class1 = new Class(100);
abstract method 와 함께 사용하기
- abstract method 는 시그니처만 정의하는 메서드다.
- 구현은 상속받은 클래스에서 한다.
abstract class AbstractWithAbstractMethod {
abstract abstract_method(): string;
}
class Class2 extends AbstractWithAbstractMethod {
abstract_method(): string {
return "HELLO";
}
}