Skip to content
dreamcode
dreamcode
Map
Inheritance
Lesson 29 of 48
+15 XP on finish
JS CLASSESChapter 5 · JS Functions and Classes

Class Inheritance

class Comet extends SpaceObject creates a class that inherits every method of SpaceObject. In the child's constructor, call super(...) before you use this. Override a method by defining it again, and reach the parent's version with super.method(). instanceof checks the whole chain of parents.

Worked example

How it reads

  • extends sets up the parent class
  • super(name) runs the parent constructor first
  • super.describe() calls the version the child is overriding
Common mistakes
  • Using this in a child constructor before calling super() throws a ReferenceError.
Cloud tip: Keep inheritance shallow. One or two levels is easy to follow; five is a maze.
index.js
JAVASCRIPT
real JavaScript, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Add a Dog class that extends Animal and overrides speak so new Dog("Rex").speak() returns Rex says woof. Print it.

Press Run to check your work.