Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a new class (known as the derived class or child class) to acquire properties and behaviors (member variables and member functions) from an existing class (known as the base class or parent class).
The primary goals of inheritance are:
- Code Reusability: Prevents writing the same code multiple times, promoting efficiency.
- Establishing Relationships: Creates an "is-a" relationship between classes (e.g., a "Car is a Vehicle").
- Polymorphism: Enables runtime flexibility through method overriding.
Definition of Inheritance: Inheritance is a mechanism in object-oriented programming that allows a new class (derived class) to inherit properties and behaviors (member variables and member functions) from an existing class (base class). It promotes code reusability and establishes an "is-a" relationship between classes.
Defining Derived Classes
A derived class is declared using a colon (`:`) followed by the access specifier and the name of the base class. The syntax is as follows:
class DerivedClass : access_specifier BaseClass {
// Members of DerivedClass
};
Here, access_specifier can be public, protected, or private. This specifier controls how the public and protected members of the base class are inherited by the derived class.
publicInheritance:publicmembers of the base class becomepublicin the derived class, andprotectedmembers becomeprotected.protectedInheritance:publicandprotectedmembers of the base class both becomeprotectedin the derived class.privateInheritance:publicandprotectedmembers of the base class both becomeprivatein the derived class.
In all types of inheritance, private members of the base class are never directly accessible in the derived class.
Making a Private Member Inheritable
In C++, private members of a base class are not directly accessible to derived classes. This means a derived class cannot directly read or modify a private member inherited from its base class.
However, there are ways to manage access to base class members:
- Using
protectedAccess Specifier: The primary mechanism to make base class members "inheritable" in the sense of being accessible to derived classes (but still hidden from outside access) is to declare them asprotectedin the base class. - Indirect Access via Base Class Methods: If a member must remain
privatein the base class, derived classes can still interact with it indirectly by callingpublicorprotectedmember functions of the base class that provide access (e.g., getter and setter methods).
Let's compare the visibility of base class members based on their access specifier and the type of inheritance:
| Base Class Member Type | Public Inheritance | Protected Inheritance | Private Inheritance | Outside Class Hierarchy |
|---|---|---|---|---|
public member | public | protected | private | Accessible |
protected member | protected | protected | private | Not Accessible |
private member | Not Accessible | Not Accessible | Not Accessible | Not Accessible |
Observation: The private members of the base class are never directly accessible to any derived class, regardless of the inheritance type. Their existence and functionality are managed entirely within the base class itself.
Types of Inheritance
Single Inheritance
Single inheritance is the simplest form of inheritance where a derived class inherits from only one base class.
Definition of Single Inheritance: In single inheritance, a derived class inherits from only one base class. This is the simplest form of inheritance.
Structure:
BaseClass ↑DerivedClass
Example: A Car class inheriting from a Vehicle class.
Multilevel Inheritance
In multilevel inheritance, a class is derived from another derived class, forming a chain or hierarchy. This means one class serves as a base class for another, which in turn becomes a base class for a third, and so on.
Definition of Multilevel Inheritance: In multilevel inheritance, a class is derived from another derived class. This creates a hierarchy where one class serves as a base class for another, which in turn becomes a base class for a third, and so on.
Structure:
Grandparent ↑Parent ↑Child
Example: A SportsCar class inheriting from a Car class, which in turn inherits from a Vehicle class.
Hierarchical Inheritance
Hierarchical inheritance occurs when one base class is inherited by multiple derived classes. This means several classes share a common base class.
Definition of Hierarchical Inheritance: In hierarchical inheritance, one base class is inherited by multiple derived classes. This means several classes share a common base class.
Structure:
BaseClass ↗ ↘Derived1 Derived2
Example: A Vehicle class being inherited by Car, Bike, and Truck classes.
Multiple Inheritance
Multiple inheritance allows a single derived class to inherit from two or more base classes. It combines the features of all its base classes into one new class.
Definition of Multiple Inheritance: In multiple inheritance, a single derived class inherits from two or more base classes. It combines the features of all its base classes.
Structure:
Base1 Base2 ↘ ↙ DerivedClass
Example: A FlyingCar class inheriting from both Car and Airplane classes.
Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance (e.g., a mix of single, multiple, hierarchical, or multilevel inheritance). It is often used to achieve complex design requirements.
Definition of Hybrid Inheritance: Hybrid inheritance is a combination of two or more types of inheritance. It often involves a mix of single, multiple, hierarchical, or multilevel inheritance to achieve specific design requirements.
Structure (Example combining hierarchical and multiple inheritance):
Vehicle ↗ ↘Car Boat ↘ ↙ AmphibiousVehicle
In this example, Car and Boat both inherit from Vehicle (hierarchical), and AmphibiousVehicle inherits from both Car and Boat (multiple).