# Inheritance & Polymorphism

상속은 OOP 개념 중 하나이다. 상속은 말그대로 해당 클래스의 모든 내용을 다른 클래스에 그대로 복사한다는 의미다. 즉, Food 클래스의 프로퍼티 `this.name`과 메소드 `getName()`을 다른 클래스에 그대로 상속할 수 있다.

```javascript
// Food.js
class Food{
    constructor(name) {
        this.name = name;
    }
    
    getName() {
        return this.name;
    }
}

export default Food;
```

```javascript
// Dog.js
import Food from './Food';

class Deep extends Food{
    constructor(name) {
        super(name);
    }
}

export default Deep;
```

`extends` 키워드를 사용해 Deep클래스가 Food 클래스를 상속했다. 이제 Food 클래스는 Deep 클래스의 superclass가 되었고, Deep 클래스는 Food 클래스의 subclass가 되었다. Deep 클래스는 Food 클래스가 가지고 있는 `this.name`과 `getName()`을 똑같이 갖는다.

subclass의 constructor에는 `super()`를 넣어 superclass의 constructor를 호출할 수도 있다. subclass에서 `super()`를 사용하지 않아도 되는 경우 에러가 발생하지는 않지만, 그래도 `super()`를 명시하길 권장한다.

```javascript
// index.js
import Deep from './Deep';

let jake = new Deep ('Jake');

console.log(jake.getName()); // 'Jake'
```

&#x20;이런 식으로 사용한다. Dog 인스턴스 `jake`가 Animal 클래스의 `getName()`을 호출한다.
