Multiple inheritance in object orientation is a form of generalization which allows a class to inherit features from multiple super class. Using the concept of multiple inheritance, you can derive an object that incorporates the information from multiple classes.
Multiple inheritance elevates the potential of programming languages and increases the possibility of reusing the code. In this section, we will be discussing the concept of multiple inheritance in deep along with its kinds and workarounds.
Content: Multiple Inheritance in Object Orientation
- What is Multiple Inheritance?
- Forms of Multiple Inheritance
- Workarounds for Multiple Inheritance
- Key Takeaways
What is Multiple Inheritance?
Multiple inheritance in object orientation is a concept of object modelling where a class is permitted to have multiple parent classes. The child class inherits characteristics from all the parent classes.
Multiple inheritance is considered as a complex form of generalization when you compare it with single inheritance. As in single inheritance a sub class have only one super class but with multiple inheritance a sub class have multiple super classes.
The multiple inheritance increases the opportunity of code reusability as all the generalized features will be mentioned once in the parent classes and child class will inherit those features from their parent classes.
Multiple inheritance is vaguely described as the conceptual relationship between the classes in the class model and also as a language mechanism that is used to accomplish the relationship between the classes in the programs.
Let us take a handier example of multiple inheritance. Consider there are two super classes Father and Mother and a sub class, ‘Child’ inheriting the features of these two super classes.
We all know a child always inherits feature of his parents.
Forms of Multiple Inheritance
Now, we will discuss two forms of multiple inheritance i.e. multiple inheritance from disjoint classes and multiple inheritance from overlapping classes. Initiating with the multiple inheritance from disjoint classes, it is considered as the most common form of multiple inheritance.
Multiple Inheritance from Disjoint Classes
Multiple inheritance from the set of disjoint classes allows a sub class to have each of its super class from a different set of disjoint classes. Now the question arises what are disjoint classes?
1. Disjoint class
The two classes are said to be disjoint when there is no instance that can belong to both of these classes at a time. To understand this, we will take the help of an example. In the figure below, the classes FullTimeEmployee and PartTimeEmpolyee are the disjoint classes as an employee at one time can either be a full-time employee or he can be a part time employee, he can not be both at a time. So, we conclude that the classes FullTimeEmployee and PartTimeEmpolyee are disjoint classes.
Now, back to multiple inheritance from disjoint classes, we have studied that it permits a sub class to inherit classes from different disjoint sets. Like, in the example above the class PartTimeManager inherits each class from the disjoint set. Well, there can more such classes such as FullTimeIndividualContributor, PartTimeIndividualContributor and FullTimeManager.
If a class can be explored on different independent aspects then you must implement multiple generalization on that class. Like we have refined the class Employee on two aspects, i.e. on the basis of employment status and on the managerial status.
Observe one thing that the sub class PartTimeManager inherits its feature from the same ancestor class i.e. from the class Employee along two different paths. One path is via employment status and the other path is via managerial status. Still, the sub class PartTimeManager has a single copy of the Employee class feature.
There can arise an ambiguity while inheriting multiple classes. Like in our above example, when a sub class PartTimeIndividualContributor inherits two super classes PartTimeEmployee and IndividualContributor. Now, both these super class have the attribute ‘name’.
But there is a problem here as the name attribute in the PartTimeEmployee would denote the person name. And the name attribute in IndividualContributor would denote the person’s title.
To avoid this kind of ambiguity the attributes must be stated explicitly such as PartTimeEmployee.PersonName and IndividualContributor.Title.
3. Multiple Inheritance from Overlapping Classes
There can occur multiple inheritance with the overlapping classes. Now, what are the overlapping classes? The two sub classes are the overlapping classes if they can behave in both aspects of the super class.
Let us take an example, as you can see in the figure below the class LandVehicle and the class WaterVehicle are the overlapping classes as there are some vehicles that can travel on both, land as well as water. So, a vehicle can be both lands as well as water vehicle.
The sub class AmphibiousVehicle inherits two super classes which are overlapping classes, it shows an example of Multiple inheritance from overlapping classes.
Workarounds for Multiple Inheritance
While designing the class model if you sense the absence or lack of multiple inheritance then it could be the implementation fault. Well, this could be resolve while early restructuring of the class model. In the section below we will list out some restructuring techniques.
1. Delegation Using Composition of Parts
Implementing delegation means allowing objects to forward their operation to another object for execution. For example, look at the figure below the Employee class is a composition of EmployeeEmployment and EmployManagement. Whenever the object of Employee class is operated, the operation would be redirected either to EmployeeEmployment or EmployeeManagment.
This denotes delegation using the composition of parts.
2. Inherit the Important and Delegate the Rest
The important generalization is preserved and remaining generalization is degraded while their operations are delegated to the other object.
3. Nested Generalization
If a class can be explored in many aspects then explore it on one aspect completely first and then explore the other. This approach retrieves all the possible combinations.
4. Super Classes of Equal Importance
A subclass may have more than one super class and all may be of equal importance then using delegation is the best method to preserve the symmetry of the model.
5. Dominant Super Class
If there is one dominating superclass then preserve the inheritance through that path.
6. Few Subclasses
In the class model, if there are few subclasses then opt for nested generalization. For the larger subclasses avoid generalization.
7. Sequencing Generalization Sets
The nested generalization must be implemented sequentially, the most important aspect is explored first, then second and so on.
8. Large Quantities of Code
The nested generalization reduces the quantities of code.
Preserve the identity in the class model using a nested generalization.
- Multiple inheritance is a complex generalization.
- In multiple inheritance a sub class can inherit several super classes.
- Multiple inheritance can be implemented from disjoint classes and overlapping classes.
- The lack of multiple inheritance must be resolved in the early restructuring of the class model.
So, this all about multiple inheritance in object orientation. We have also seen different form of multiple inheritance along with its workarounds.
Leave a Reply