6. Inheritance
Table of Contents:
Inheritance and "Is A" Relationships
Inheritance is a pillar of object-oriented programming. It describes a relationship between two classes: a subclass that inherits methods from a superclass. As a result, instances of the sub-class can use methods defined in a super-class.
We call this an "Is A" relationship

Array is a Subclass of Object
The Array
class is a sub-class of the Object
class which is the super-class.
Every Array instance gets methods from the Array.prototype
which inherits methods from the Object.prototype
. Therefore, all arrays can use Object.prototype
methods like toString()
.
Try running the following code:
const arr = [1,2,3];
console.log(arr.toString());
console.log(arr); // expand the prototype chain to find the .toString() method
console.log(typeof arr);
console.log(arr instanceof Array);
console.log(arr instanceof Object);
Establishing Inheritance Between Custom Classes
Imagine we have a Person
class. I want to make another class called Programmer
that can do everything a Person
can, with some additional properties and behaviors that only instances of Programmer
will have.
How NOT to do it:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
this.friends = [];
}
makeFriend(friend) {
this.friends.push(friend)
console.log(`Hi ${friend}, my name is ${this.name}, nice to meet you!`);
}
doActivity(activity) {
console.log(`${this.name} is ${activity}`);
}
}
class Programmer {
constructor(name, age, language) {
this.name = name;
this.age = age;
this.friends = [];
this.favoriteLanguage = language;
}
makeFriend(friend) {
this.friends.push(friend)
console.log(`Hi ${friend}, my name is ${this.name}, nice to meet you!`);
}
doActivity(activity) {
console.log(`${this.name} is ${activity}`);
}
code() {
this.doActivity(`writing some ${this.favoriteLanguage} code.`);
}
}
Extends and Super
To remove the repetitive code AND to establish a relationship between Programmer
and Person
, we use the extends
and super
keywords to define our Programmer
class:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
this.friends = [];
}
makeFriend(friend) {
this.friends.push(friend)
console.log(`Hi ${friend}, my name is ${this.name}, nice to meet you!`);
}
doActivity(activity) {
console.log(`${this.name} is ${activity}`);
}
}
class Programmer extends Person {
constructor(name, age, language) {
super(name, age); // invoke the Person constructor, setting the name, age, and friends properties on `this`
this.favoriteLanguage = language; // add a favoriteLanguage property only for Programmers
}
// makeFriend is inherited
// doActivity is inherited
code() { // a new method only Programmer instances can use
this.doActivity(`writing some ${this.favoriteLanguage} code.`);
}
}
Refactor Challenge
Create another subclass called WebDeveloper
that extends the Programmer
class. It should have its favoriteLanguage
be set to "JavaScript"
by default.
Add a deploy
method to the WebDeveloper
class that leverages the inherited doActivity
method to print out a message saying [Name] is deploying.
Then, with a partner, discuss these questions:
Summary
Inheritance occurs when a child class inherits properties and methods from a parent class
The
extends
keyword creates this relationshipThe
super
keyword references the parent classYou can invoke
super()
to invoke the parent class constructor.
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
this.friends = [];
}
makeFriend(friend) {
this.friends.push(friend)
console.log(`Hi ${friend}, my name is ${this.name}, nice to meet you!`);
}
doActivity(activity) {
console.log(`${this.name} is ${activity}`);
}
}
class Programmer extends Person {
constructor(name, age, language) {
super(name, age); // invoke the Person constructor, setting the name, age, and friends properties on `this`
this.favoriteLanguage = language; // add a favoriteLanguage property only for Programmers
}
// makeFriend is inherited
// doActivity is inherited
code() { // a new method only Programmer instances can use
this.doActivity(`writing some ${this.favoriteLanguage} code.`);
}
}
Last updated