추상 클래스(Abstract Class)란?

추상 클래스(Abstract Class) 예시

abstract class Mammal {
  constructor(
    protected name: string,
    protected residence: string
  ) {}

  // 일반 메소드 : 모든 자식이 공통으로 사용할 기능 (오버라이딩이 비필수)
  public breath(): void {
    console.log(`${this.name}이/가 폐호흡합니다.`);
  }

  // 추상 메소드 : 자식쪽에서 무조건 오버라이딩 하도록 강제
  abstract printResidence(): void;
}

class Whale extends Mammal {
  constructor(name: string, residence: string) {
    super(name, residence);
  }

  // 추상 메소드를 오버라이딩하여 구현 (필수)
  override printResidence(): void {
    console.log(`${this.residence}에 삽니다.`);
  }
}

// 해당 클래스는 추상 메소드를 구현하지 않았으므로 에러 발생
// Error: Non-abstract class 'FlyingSquirrel' does not implement inherited abstract member printResidence from class 'Mammal'.
class FlyingSquirrel extends Mammal {
  constructor(name: string, residence: string) {
    super(name, residence);
  }
}