Today, we’re diving into Inheritance in JavaScript, but with a twist — we’ll focus entirely on using classes. No confusion, no distractions, just the magic of inheritance made simple! Ready? Let’s begin! 🚀
Inheritance is when a class (child) takes on the properties and methods of another class (parent). Think of it like this:
It’s all about reusing code instead of rewriting it. Why do the same work twice when you can inherit it? 😉
Classes make inheritance in JavaScript:
To understand class inheritance, let’s break it into three simple steps:
// Parent class
class Animal {
constructor(name) {
this.name = name;
}
eat() {
console.log(`${this.name} is eating.`);
}
}
// Child class
class Dog extends Animal {
bark() {
console.log(`${this.name} is barking.`);
}
}
// Create objects
const animal = new Animal("Generic Animal");
animal.eat(); // Output: Generic Animal is eating.
const dog = new Dog("Buddy");
dog.eat(); // Output: Buddy is eating. (inherited from Animal)
dog.bark(); // Output: Buddy is barking.
Explanation:
Animal is the parent class with a property name and a method eat.
Dog extends Animal, meaning it inherits the eat method.
Dog adds its own method, bark, making it unique.
// Parent class
class Animal {
constructor(name) {
this.name = name;
}
eat() {
console.log(`${this.name} is eating.`);
}
}
// Child class
class Cat extends Animal {
constructor(name, color) {
super(name); // Calls the parent class's constructor
this.color = color; // Adds a new property
}
purr() {
console.log(`${this.name}, the ${this.color} cat, is purring.`);
}
}
// Create an object
const kitty = new Cat("Whiskers", "black");
kitty.eat(); // Output: Whiskers is eating.
kitty.purr(); // Output: Whiskers, the black cat, is purring.
Explanation:
The super(name) in Cat calls the constructor of the parent Animal to initialize name.
The child class Cat adds its own property, color, and method, purr.
// Parent class
class Animal {
constructor(name) {
this.name = name;
}
sound() {
console.log(`${this.name} makes a sound.`);
}
}
// Child class
class Dog extends Animal {
sound() {
console.log(`${this.name} barks.`);
}
}
// Create objects
const genericAnimal = new Animal("Generic Animal");
genericAnimal.sound(); // Output: Generic Animal makes a sound.
const dog = new Dog("Buddy");
dog.sound(); // Output: Buddy barks.
Explanation:
The sound method in Animal provides a general behavior.
The Dog class overrides it with a more specific behavior (barks).
// Parent class
class Product {
constructor(name, price) {
this.name = name;
this.price = price;
}
display() {
console.log(`${this.name} costs $${this.price}.`);
}
}
// Child class
class Book extends Product {
constructor(name, price, author) {
super(name, price); // Call the parent class's constructor
this.author = author; // Add a new property
}
display() {
super.display(); // Call the parent method
console.log(`Written by ${this.author}.`);
}
}
// Create objects
const product = new Product("Laptop", 1200);
product.display(); // Output: Laptop costs $1200.
const book = new Book("JavaScript Basics", 25, "John Doe");
book.display();
// Output:
// JavaScript Basics costs $25.
// Written by John Doe.
“Good code is not written; it’s refined.” Keep practicing, and soon, you’ll master how to organize your code using inheritance like a pro. Just like building a solid foundation for a house, inheritance makes your code sturdy and future-proof! 💪
In the next lesson, we’ll explore Polymorphism, a superpower of inheritance that allows you to use the same method in different ways. It’s going to be fun and enlightening, so stay tuned! 🌟
Keep coding, stay curious, and never stop learning. See you in the next class! 😊