Multilevel inheritance in C++ with real life analogy

Multilevel inheritance in C++ with real life analogy

5 mins read1.3K Views Comment
clickHere
Updated on Feb 9, 2023 16:48 IST

This article will explore multilevel inheritance with its advantages and disadvantages. We will learn this concept with real life analogy first and then with coding example with explanation

2023_01_MicrosoftTeams-image-25.jpg

In multilevel inheritance, a parent inherits a child, which in turn becomes the parent of another class and shares the parent’s resources with that child. In multilevel inheritance, derived classes inherit from base class, and derived classes act as base classes for other classes. We will learn this concept with real life analogy first and then with coding example with explanation.

Table of contents

What is Multilevel Inheritance?

Multilevel inheritance is a type of inheritance in object-oriented programming where one class is used as a base class for another class, which in turn serves as a base class for another class, and so on. It is a type of class inheritance that allows a class to acquire properties from multiple levels of ancestor classes. This allows for a more complex class structure, with each class inheriting from one or more ancestor classes and passing on its properties to any descendant classes.

In multilevel inheritance, a derived class is created from a base class, and the derived class then becomes the base class for a new class. This process can be repeated any number of times, with each derived class inheriting the properties of the class from which it was derived. This type of inheritance also allows for a class to override the methods or properties of any of its ancestor classes, allowing for greater flexibility in the coding process.

Real-life analogy of multilevel inheritance

Multilevel inheritance is a type of inheritance in Object-Oriented Programming (OOP) where a class can inherit properties and methods from its parent and grandparent classes. This concept can be compared to family dynamics. In a family, a child inherits both parents’ and their grandparents’ physical and mental traits. As you can see in the fig above that mother has inherited properties from he mother and grandchild is inheriting properties from her mother as well as her grandmother.Remember every derived class will have its own properties also.

In the same way, multilevel inheritance allows a class to have access to the properties and methods of several generations of classes.

When to use multilevel inheritance

1.Hierarchy of classes:It is used when you need to create a complex class structure and when you want to create a logical hierarchy for your classes.

2. Code reuse: Multilevel inheritance is a powerful tool for rapid development of complex applications. It allows the programmer to create a complex class structure and reduces the amount of code needed to create a new class. However, it can also create complexity and make it difficult to maintain the codebase. As such, it should be used with caution.

Advantages of Multilevel Inheritance

  1. Multilevel inheritance is a powerful tool in object-oriented programming, allowing developers to build upon existing classes and add greater levels of complexity. It enables developers to avoid redundancy and share common characteristics across classes.
  1. It reduces the amount of code that must be written. When objects inherit from multiple parent classes, developers do not have to write code for each class; instead, they can use the existing code from the parent classes. This helps to save time and makes it easier to maintain the code.
  2. Finally, multilevel inheritance allows developers to create a hierarchy of classes where each class is responsible for a specific task. This makes the code easier to understand and debug, as developers can easily pinpoint the source of any errors.
  3. Multilevel inheritance is an essential tool for object-oriented programming, allowing developers to create complex and efficient models quickly and easily.

Disadvantages of Multilevel Inheritance

  1. Multilevel inheritance can quickly become complex and difficult to manage, as it can easily cause a large amount of code to be written for one class. Also, it can lead to a less clear program structure since the class hierarchy can get hard to understand. Additionally, it may lead to the reuse of code which is only sometimes desirable, as different classes may require different implementations of the same methods. Finally, it may be not easy to debug since the code from multiple classes may have to be checked.

Difference between multi-level and multiple inheritance in C++

Parameter Multilevel inheritance Multiple inheritance
Used when Multilevel inheritance is when a class inherits from a class that is already the descendant of some other class. In multilevel inheritance, one class serves as the parent class for another which, in turn, serves as the parent class for another class. Multiple inheritance is when a class inherits from more than one parent class. It enables a class to inherit from multiple parent classes, so that it can access the methods and attributes of all the parent classes.
Parent class Only one parent class is present More than one parent class is present. 
Complexity Multilevel inheritance is a simpler concept,as it involves only one parent class Multiple inheritance can be complex, as it involves more than one parent class. 
Implementation Multilevel inheritance can be implemented in certain scenarios, as it reduces the complexity of the code and increases readability. Multiple inheritance can be difficult to implement, since it involves more than one parent class.

Program for multilevel inheritance in C++

 
#include <iostream>
using namespace std;
// base class
class mother
{
public:
void display()
{
cout << “mother class” << endl;
}
};
// sub class which inherits from class A
class daughter_1 : public mother
{
public:
void display()
{
cout << “daughter_1” << endl;
}
};
// sub class which inherits from class B
class daughter_2 : public mother
{
public:
void display()
{
cout << “daughter_2” << endl;
}
};
int main()
{
daughter_2 obj;
obj.display();
return 0;
}
Copy code

Output

daughter_2

 Explanation

In this we have one base class called mother and we have to derive class daughter_1 and daughter_2. Both of these classes inherit properties from the Mother class. Then in this example we created object of daughter_2 class and called display().

Conclusion

Multiple inheritance is a powerful feature of object-oriented programming that allows a child class to inherit properties from multiple parent classes. It can be used to create complex and efficient software applications; however, it can also lead to complex code and many potential software bugs. When using multiple inheritance, it is important to consider its consequences, such as the complexity of code, the risk of unexpected behavior, and the potential for conflicts between parent classes. It is advisable to use multiple inheritance judiciously, understanding the associated risks and potential benefits.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments