Introduction to Multiple Inheritance

This tutorial provides a basic introduction to the concept of multiple-inheritance. The tutorial begins with a brief explanation of Inheritance and deals with multiple inheritance in particular.

Prerequisites: A basic idea about classes and objects and Object-Oriented Programming (hereafter referred to as OOP)

What is Inheritance?

Inheritance is one of the key features of OOP. It is a mechanism that allows one class to inherit the features of another class. In other words, the derived class (the class that inherits from another class – called the base class) can make use of the data members and member functions that are defined in the base class.

Why do we need it?

Inheritance offers a lot of advantages. Some of them include:

Code Reusability

The functions and data members defined in a base class need not be declared/defined repeatedly. If another class requires these members, it merely has to inherit from the base class.

 

Error Handling

If errors are encountered in a program that uses inheritance, and the error happens to have occurred in the base class, it is sufficient to correct the error once in the base class. All the related changes will automatically reflect on all its derived classes since its base classes have inherited from the same base class. It is not necessary to make changes in multiple parts of the program.

 

Updates

New features i.e. updates can be made to the existing class without disturbing the current structure of the base class. One can simply create a derived class for it and add the new features (methods or data). This makes the entire process fo adding new features or overriding existing features (functions) very easy and fast.

 

Transitive Nature

This is one of the most critical features of inheritance itself. For instance, if class C inherits class B and B, in turn, inherits from class A, then the changes made in class A will reflect not just in B but also in A. This is the transitive nature of inheritance and again helps in finding and correcting errors. It also enables any number of updates to be made to the base class as for every new update (i.e. addition/change of features) a new derived class can be defined.

Multiple Inheritance in OOP transitive

 

Real-World Relatability

One of the most important aspects of OOP is that it establishes a closer relation to the real-world scenario i.e. it emphasizes on a scenario where there are objects of various types, each with their properties and behaviors and they interact with each other, just like in the real world. It makes programming more easily understandable and relatable than Procedure Oriented Programming (POP).

 

Multiple Inheritance

Multiple Inheritance in one type of Inheritance wherein, one class inherits from two or more base classes. Which means that it adopts features from both the classes and is a type of both. A real-world example could be that of a child. A child inherits characteristics of both his/her parents. Hence the child possesses some features from each of his/her parents. Hence, if written in a program, the child class will inherit from two classes, father and mother.

Multiple Inheritance

Although this method of inheritance is very beneficial in the sense that it can adapt features from more than one class, it requires a good design and implementation. Without that, the program becomes more complex and ambiguous.

This type of inheritance is particularly useful when an object has to adopt and add on to the properties of two very different classes that cannot be considered to be a part of one another for any practical purpose. Such classes have very different lines of characteristics. For instance, consider the case of metalloids. They possess properties of both metals and non-metals, but metals and non-metals (the base classes) hardly have any similar properties.

Feel free to leave any sort feedback, comments or suggestions for this post below.

Leave a Reply

Your email address will not be published. Required fields are marked *