Inheritance: Unlocking Code Reusability in JavaScript
Introduction to Inheritance
Greetings, coder! Our voyage into Object-Oriented Programming (OOP) continues. Today, we will cover Inheritance, which can make our code more efficient and tidy.
Inheritance: First Example
Just as children inherit traits from their parents, child classes in programming inherit behaviors and properties from parent classes.
Below is a practical example displaying a child class inheriting from a parent class:
In this example, Child inherits from Parent, so it shares the greet() method, making our code smarter!
Understanding the 'extends' Keyword
To implement inheritance, we introduce a keyword: 'extends'. This powerful keyword allows a new class to inherit properties and methods from an existing class. Let's say, a 'child' class 'extends' from the 'parent' class.
Let's see how a car can extend a vehicle. Both are entities with the potential for motion, but a car offers additional specific features — such as wheels.
In this code, the Car class extends from the Vehicle class, sharing the move() method.
