Developer Dictionary
Inheritance
Definition
A mechanism where a new class is derived from an existing class.
Deep Dive
In object-oriented programming (OOP), inheritance is a fundamental mechanism that allows a new class to be derived from an existing class. The new class, known as the subclass (or derived class), automatically acquires all the non-private properties (attributes) and behaviors (methods) of the existing class, which is referred to as the superclass (or base class). This powerful concept promotes code reusability by enabling developers to build upon existing functionality rather than rewriting it from scratch.
Examples & Use Cases
- 1A `Car` class inheriting from a `Vehicle` class, gaining common properties like `make`, `model`, and methods like `startEngine()` or `accelerate()`, while adding specific car-related behaviors.
- 2A `Manager` class inheriting from an `Employee` class, thereby acquiring shared attributes like `name`, `employeeID`, and `salary`, but adding its own unique methods such as `manageTeam()` or `approveLeave()`.
Related Terms
Object-Oriented Programming (OOP)ClassObjectPolymorphism