Breaking News

Multiple Inheritance In C++

One important object-oriented mechanism is multiple inheritance. Multiple inheritance does not mean that multiple subclasses share the same superclass.
 
It also does not mean that a subclass can inherit from a class which itself is a subclass of another class.
 
Multiple inheritance means that one subclass can have more than one superclass. This enables the subclass to inherit properties of more than one superclass and to “merge” their properties.
 
As an example consider again our drawing program. Suppose we already have a class String which allows convenient handling of text.
 
For example, it might have a method to append other text. In our program we would like to use this class to add text to the possible drawing objects.
 
It would be nice to also use already existing routines such as move() to move the text around.
 
Consequently, it makes sense to let a drawable text have a point which defines its location within the drawing area.
 
Therefore we derive a new class DrawableString which inherits properties from Point and String as illustrated in Figure below:
 
 
Definition (Multiple Inheritance) If class A inherits from more than one class, i.e.. A inherits from B1, B2, …, Bn, we speak of multiple inheritance.
 
This may introduce naming conflicts in A if at least two of its superclasses define properties with the same name.
 
The above definition introduce naming conflicts which occur if more than one superclass of a subclassuse the same name for either attributes or methods.
 
For an example, let’s assume, that class String defines a method setX() which sets the string to a sequence of “X” character.
 
The question arises, what should be inherited by DrawableString? The Point, String version or none of them?
 
These conflicts can be solved in at least two ways:
 
The order in which the superclasses are provided define which property will be accessible by the conflict causing name. Others will be “hidden”.
 
The subclass must resolve the conflict by providing a property with the name and by defining how to use the ones from its superclasses.
 
The first solution is not very convenient as it introduces implicit consequences depending on the order in which classes inherit from each other. For the second case, subclasses must explicitly redefine properties which are involved in a naming conflict.
 
A special type of naming conflict is introduced if a class D multiply inherits from superclasses B and C which themselves are derived from one superclass A. This leads to an inheritance graph as shown in Figure below:
 
A name conflict introduced by a shared superclass of superclasses used with multiple inheritance.
 
 
The question arises what properties class D actually inherits from its superclasses B and C.
 
Some existing programming languages solve this special inheritance graph by deriving D with the properties of A plus the properties of B and C without the properties they have inherited from A.
 
Consequently, D cannot introduce naming conflicts with names of class A. However, if B and C add properties with the same name, D runs into a naming conflict.
 
Another possible solution is, that D inherits from both inheritance paths. In this solution, D owns two copies of the properties of A: one is inherited by B and one by C.
 
Although multiple inheritance is a powerful object-oriented mechanism the problems introduced with naming conflicts have lead several authors to “doom” it.
 
As the result of multiple inheritance can always be achieved by using (simple) inheritance some object-oriented languages even don’t allow its use.
 
However, carefully used, under some conditions multiple inheritance provides an efficient and elegant way of formulating things.
 
There are some more types of inheritance is used in the nature of programming, some of them is given and discussed below:
 
Hierarchical Inheritance
 
This is another interesting application of inheritance is to use it as a support to the hierarchical design of a program.
 
In this type of approach many programming problems can be cast into a hierarchy where certain features of one level are shared by many others below that level.
 
Hybrid Inheritance
 
There could be the situations where we need to apply two or more types of inheritance to design a program. In this case we use Hybrid Inheritance.

Check Also

Insertion & Extraction In C++

Insertion is the operation of sending characters to a stream, expressed by the overloaded insertion …