> For the complete documentation index, see [llms.txt](https://triplexlab.gitbook.io/vanilla-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://triplexlab.gitbook.io/vanilla-js/basics/09-prototype-class/abstract-class.md).

# Abstract Class

추상 클래스

Food클래스가 분명 존재하지만, 단순히 '동물’을 만든다는 것은 조금 이상한 일이다. 동물은 추상적인 개념이기 때문에 Animal 객체를 생성하는 일이 있어서는 안 된다. 이럴 때 추상화(Abstraction)를 통해 `new Food(...);`과 같은 명령을 미연에 방지할 수 있다. Java의 경우 `public abstract class Animal {...}`과 같은 방식으로 추상 클래스를 만들 수 있다. 아쉽지만 자바스크립트에서는 추상 클래스나 메소드를 만들 수 없다. 다만 추상 메소드를 직접 구현하는 방법은 있다.

```javascript
// Food.js
class Food{
    constructor(name) {
        this.name = name;
    }
    
    getName() {
        return this.name;
    }
    
    // Abstract
    makeNoise() {
        throw new Error('makeNoise() must be implement.');
    }
}

export default Food;
```

`makeNoise()`를 추상 메소드로 만들어 subclass에서 구현되지 않은, \
Food클래스에서 `makeNoise()`를 호출하면 에러를 발생시키도록 했다. \
이 경우 추상 메소드는 반드시 subclass에서 오버라이드되어야 한다.

추상 클래스를 만드는 것을 조금 더 번거롭다. 직접 Abstract 클래스를 만들어 상속시키는 방식인데, 스택오버플로우의 [Does ECMAScript 6 have a convention for abstract classes?](https://stackoverflow.com/questions/29480569/does-ecmascript-6-have-a-convention-for-abstract-classes)를 참고해보자.
