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
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?
- Real-life analogy of multilevel inheritance
- When to use multilevel inheritance
- Advantages of Multilevel Inheritance
- Disadvantages of Multilevel Inheritance
- Difference between multi-level and multiple inheritance in C++
- Program for multilevel inheritance in C++
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
- 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.
- 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.
- 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.
- 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
- 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++
|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.
|Only one parent class is present
|More than one parent class is present.
|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.
|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++
using namespace std;
// base class
cout << “mother class” << endl;
// sub class which inherits from class A
class daughter_1 : public mother
cout << “daughter_1” << endl;
// sub class which inherits from class B
class daughter_2 : public mother
cout << “daughter_2” << endl;
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().
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.