Inheritance
MediumIn plain terms
Classes inherit from other classes with extends. super() must be called in constructor before using this. super.method() calls parent methods. Override by redefining.
What you need to know
- •extends
- •super() in constructor
- •Method override
Try it yourself
Copy the code below and run it in your browser console or a code editor:
class Animal {
constructor(name) { this.name = name; }
speak() { return this.name + ' makes a sound'; }
}
class Dog extends Animal {
constructor(name) { super(name); }
speak() { return this.name + ' barks'; }
}Learn more
Dive deeper with these trusted resources: