Welcome, JavaScript adventurers! In this chapter, we delve into the concept of inheritance, a cornerstone of object-oriented programming (OOP) in JavaScript. Inheritance allows you to create new classes (subclasses) that inherit properties and behaviors from existing classes (superclasses). Buckle up as we explore the fundamentals, practical applications, and advanced techniques of inheritance in JavaScript classes.
extends
keyword is used to establish inheritance between classes.
class Animal {
constructor(name) {
this.name = name;
}
eat() {
console.log(this.name + " is eating.");
}
}
class Dog extends Animal { // Dog inherits from Animal (subclass)
constructor(name, breed) {
super(name); // Call the superclass constructor (Animal)
this.breed = breed;
}
bark() {
console.log(this.name + " is barking!");
}
}
const dog = new Dog("Buddy", "Labrador");
dog.eat(); // Output: Buddy is eating. (inherited from Animal)
dog.bark(); // Output: Buddy is barking! (specific to Dog)
Animal
class defines a basic structure for animals with a name and an eat()
method.Dog
class inherits from Animal
using extends
.Dog
constructor calls the superclass constructor (super(name)
) to initialize the inherited name
property.Dog
also has its own breed
property and a specific bark()
method.dog
object instance.dog.eat()
executes the inherited eat()
method from Animal
.dog.bark()
executes the method specific to the Dog
class.
class Bird extends Animal {
constructor(name, wings) {
super(name);
this.wings = wings;
}
eat() {
console.log(this.name + " is pecking at food."); // Overridden eat() method
}
fly() {
console.log(this.name + " is flying!");
}
}
const bird = new Bird("Tweety", 2);
bird.eat(); // Output: Tweety is pecking at food. (overridden)
bird.fly(); // Output: Tweety is flying! (specific to Bird)
Bird
class inherits from Animal
.Bird
class overrides the eat()
method with a bird-specific eating behavior.Bird
class also has its own fly()
method.bird
object instance.bird.eat()
executes the overridden eat()
method from Bird
.bird.fly()
executes the method specific to the Bird
class.
function makeSound(animal) {
animal.makeSound(); // Call the makeSound() method (assumed to be inherited)
}
const dog = new Dog("Buddy", "Labrador");
const bird = new Bird("Tweety", 2);
makeSound(dog); // Output: Buddy is barking! (Dog's implementation of makeSound())
makeSound(bird); // Output: Tweety is chirping! (Bird's implementation, not shown here)
makeSound
function that expects an animal object as an argument.makeSound()
method (inherited from a common ancestor perhaps).makeSound(dog)
, the Dog
class’s bark()
method is executed (assuming bark()
is its implementation of `makeSoundmakeSound(bird)
, a bird-specific makeSound()
method (not shown here) would be executed, assuming the Bird
class also inherits a makeSound()
method from a common ancestor.
const Flyable = {
fly() {
console.log(this.name + " is flying!");
}
};
class Bat {
constructor(name) {
this.name = name;
}
}
Object.assign(Bat.prototype, Flyable); // Mixin Flyable functionality into Bat
const bat = new Bat("Echo");
bat.fly(); // Output: Echo is flying!
Flyable
object is a mixin with a fly()
method.Object.assign
to mix the Flyable
functionality (properties and methods) into the Bat
class prototype.Bat
instances can access the fly()
method from the mixin.Inheritance is a powerful tool for code reusability and organization in JavaScript. By understanding how to create class hierarchies, override methods, and leverage polymorphism, you can write well-structured and maintainable object-oriented programs. Happy coding !❤️