Explore the power of ES6 class inheritance in JavaScript. Learn how to create subclasses using extends, utilize super in constructors and methods, and understand the principles of object-oriented programming.
Welcome to the world of ES6 class inheritance in JavaScript! In this section, we will delve into the concept of inheritance, a cornerstone of object-oriented programming (OOP). We’ll explore how ES6 classes allow us to create subclasses using the extends
keyword and how the super
keyword facilitates interaction between parent and child classes. By the end of this chapter, you’ll have a solid understanding of how to leverage inheritance to create more organized and reusable code.
Inheritance is a fundamental concept in object-oriented programming that allows one class to inherit properties and methods from another. This enables code reuse and a hierarchical class structure, where more specific classes (subclasses) extend more general ones (superclasses).
extends
In ES6, the class
keyword provides a more intuitive syntax for creating classes and implementing inheritance. To create a subclass, we use the extends
keyword, which establishes a prototype chain between the subclass and its superclass.
class ParentClass {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello, my name is ${this.name}`);
}
}
class ChildClass extends ParentClass {
constructor(name, age) {
super(name); // Call the parent class constructor
this.age = age;
}
displayAge() {
console.log(`I am ${this.age} years old`);
}
}
const child = new ChildClass('Alice', 10);
child.greet(); // Output: Hello, my name is Alice
child.displayAge(); // Output: I am 10 years old
In this example, ChildClass
extends ParentClass
, inheriting its properties and methods. The super
keyword is used to call the constructor of the parent class, ensuring that the name
property is initialized correctly.
super
The super
keyword plays a crucial role in class inheritance. It allows us to call functions on an object’s parent class. In constructors, super
is used to call the parent class’s constructor, and in methods, it can be used to access parent class methods.
super
in ConstructorsWhen a subclass has its own constructor, it must call super()
before using this
. This is because the parent class’s constructor needs to be executed to initialize the inherited properties.
class Animal {
constructor(species) {
this.species = species;
}
}
class Dog extends Animal {
constructor(name, breed) {
super('Dog'); // Call the parent class constructor
this.name = name;
this.breed = breed;
}
describe() {
console.log(`${this.name} is a ${this.breed} ${this.species}`);
}
}
const myDog = new Dog('Buddy', 'Golden Retriever');
myDog.describe(); // Output: Buddy is a Golden Retriever Dog
In this example, Dog
extends Animal
, and super('Dog')
ensures that the species
property is set correctly.
super
in MethodsBesides constructors, super
can also be used to call methods from the parent class within overridden methods in the subclass.
class Vehicle {
start() {
console.log('Vehicle started');
}
}
class Car extends Vehicle {
start() {
super.start(); // Call the parent class method
console.log('Car engine started');
}
}
const myCar = new Car();
myCar.start();
// Output:
// Vehicle started
// Car engine started
Here, the Car
class overrides the start
method of the Vehicle
class but still calls the parent class’s start
method using super.start()
.
Let’s explore some practical examples to solidify our understanding of ES6 class inheritance.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}
class Cat extends Animal {
speak() {
console.log(`${this.name} meows.`);
}
}
const dog = new Dog('Rex');
const cat = new Cat('Whiskers');
dog.speak(); // Output: Rex barks.
cat.speak(); // Output: Whiskers meows.
In this example, Dog
and Cat
are subclasses of Animal
, each overriding the speak
method to provide specific behavior.
class Shape {
constructor(color) {
this.color = color;
}
describe() {
console.log(`A shape with color ${this.color}`);
}
}
class Circle extends Shape {
constructor(color, radius) {
super(color);
this.radius = radius;
}
area() {
return Math.PI * this.radius ** 2;
}
describe() {
super.describe();
console.log(`It's a circle with radius ${this.radius}`);
}
}
const circle = new Circle('red', 5);
circle.describe();
// Output:
// A shape with color red
// It's a circle with radius 5
console.log(`Area: ${circle.area()}`); // Output: Area: 78.53981633974483
Here, Circle
extends Shape
, adding a radius
property and an area
method. It also overrides the describe
method to include additional information.
To better understand how class inheritance works, let’s visualize the relationship between classes using a diagram.
classDiagram class Animal { +String name +speak() } class Dog { +speak() } class Cat { +speak() } Animal <|-- Dog Animal <|-- Cat
Diagram Explanation: This diagram represents the inheritance hierarchy of the Animal
, Dog
, and Cat
classes. Dog
and Cat
inherit from Animal
, each providing their own implementation of the speak
method.
To reinforce your understanding, try modifying the examples above:
Animal
, such as Bird
, and implement a unique speak
method.Rectangle
class that extends Shape
, including properties for width
and height
, and methods to calculate the area and perimeter.While working with ES6 class inheritance, it’s important to be aware of common pitfalls and follow best practices:
super()
: In a subclass constructor, always call super()
before accessing this
. Failing to do so will result in a runtime error.super.methodName()
to call the parent method if needed.For more information on ES6 class inheritance, consider exploring the following resources:
Before we wrap up, let’s review some key concepts:
extends
keyword?super
keyword function in constructors and methods?Congratulations! You’ve now learned how to use ES6 class inheritance to create more organized and reusable code. By leveraging the power of extends
and super
, you can build complex class hierarchies that enhance code maintainability and readability. Remember, this is just the beginning of your journey into object-oriented programming in JavaScript. Keep experimenting, stay curious, and enjoy the process of mastering JavaScript classes!
Remember, mastering ES6 class inheritance is a significant step in your JavaScript journey. Keep practicing and exploring new concepts to deepen your understanding and enhance your coding skills!