In programming, inheritance refers to the mechanism where a class acquires the attributes and behaviors of another class. The class from which properties are inherited is known as the Base class or the parent class. On the other hand, the class that receives these inherited properties is referred to as the derived class.
For instance, just like a son inherits the traits of his father, this guide provides a concise overview of hybrid inheritance and some instances of its application.
Definition
Bringing together different forms of inheritance such as multiple, single, and hierarchical inheritance is referred to as hybrid inheritance.
In basic inheritance, a class extends another single class that serves as its foundation. In multiple inheritances, a class extends two classes, with one of them possibly being an extension of another class. In hierarchical inheritance, a single base class gives rise to multiple derived classes.
In hybrid inheritance, a blend of one or more types of inheritance occurs. For example, the amalgamation of single and hierarchical inheritance. Hence, hybrid inheritance is alternatively referred to as multipath inheritance.
Example
The illustration depicts hybrid inheritance, which combines characteristics of both single and multiple inheritance.
Class B inherits properties and behaviors from class A, illustrating single inheritance.
Class D inherits from both classes B and C, showcasing an illustration of multiple inheritance.
Syntax code of the above example
Class A
{
statement(s)
}:
Class B: public A
{
statement(s);
};
Class C
{
statement(s);
};
Class D: public B, public C
{
statement(s);
};
This instance illustrates the amalgamation of multilevel and singular inheritance.
Multilevel inheritance - As illustrated in the diagram above, Class B derives from class A, and class C inherits from class B. This demonstrates a scenario of multilevel inheritance.
Class D inherits from class B in the diagram shown above, illustrating a clear instance of single inheritance.
Syntax code for the above example
Class A
{
statement(s);
};
Class B: public A
{
statement(s);
};
Class C: public B
{
statement(s);
};
Class D: public B
{
statement(s);
};
A real-life example of hybrid inheritance
In a practical situation, we commonly operate an Automobile. Therefore, Automobile is a subclass falling within the broader Vehicle class, illustrating single inheritance.
When considering the Ferrari, it blends characteristics of both a racing car and a conventional vehicle. Therefore, the Ferrari class inherits from both the Car class and the Racing class.
Therefore, the aforementioned instance showcases both single and multiple inheritance, illustrating a clear case of hybrid inheritance combining both single and multiple aspects.
#include <iostream>
using namespace std;
class vehicle
{
public:
vehicle()
{
cout<< "This is a vehicle\n";
}
};
class Car: public vehicle
{
public:
Car()
{
cout<< "This is a car\n";
}
};
class Racing
{
public:
Racing()
{
cout<< "This is for Racing\n";
}
};
class Ferrari: public Car, public Racing
{
public:
Ferrari()
{
cout<< "Ferrari is a Racing Car\n";
}
};
int main() {
Ferrari f;
return 0;
}
Output
This is a vehicle
This is a car
This is for Racing
Ferrari is a Racing Car