Thursday, August 2, 2012

Diamond Inheritance problem in C++ and How it is solved in Java

In this tutorial, we can examine the problem with multiple inheritance in C++ language and how it is avoided in java using Interface concepts. Before going to the actual discussion, let us check out some basics about the multiple Inheritance concept.
What is multiple Inheritance?
Deriving a sub-class from two or more super classes is called multiple Inheritance. From the figure 1.1 , the sub classes ‘C’ is derived from two super classes (A and B). In multiple Inheritance there can be one or more super classes.

Figure 1.1 Deriving sub class C from two super classes (A and B)

Problem with multiple Inheritance
Consider a super class A from which two sub classes (B and C) are derived as shown in figure 1.2. The class A has a variable ‘x’  and its values is ‘5’ (x = 5). All the properties (data and methods) of super class A are derived into its subclasses B and C. Therefore the sub classes B and C have the variable ‘x’. Let us suppose, the x value in class B is changed to ‘10’ (k = 10) and in class C the variable x value is left unchanged. This is single Inheritance (one Boss many Employees). Till now there is no problem.
Problem is here:
If another sub class (D) is derived from two super classes (B and C) then all the properties if B and C are derived into the class D. The variable ‘x’ also derived into the class D. Now the problem is which value of ‘x’ should be taken into class D. This type of Inheritance is called Diamond Inheritance and leads to confusion regarding which copy of ‘x’ is available in C.
This can better explained by using Boss Employee example. Consider there are two bosses with same level priority in an organization giving orders to single employee. The employee may get confused which orders has to follow first. This sort of confusion may lead to chaos in the organization.

Figure 1.2 Diamond Inheritance
Multiple Inheritance is available in C++, but it is not available in java due to diamond Inheritance problem.
Multiple Inheritance is a powerful concept, it helps a lot while developing an application. For example, in a car manufacture company all individual parts are designed separately in different sections. But finally all these individual parts are assembled together and send it to market.
Similarly, while developing a software application, difference parts of the applications are developed separately and finally assembled together. In such cases multiple inheritance concept is very useful.

Solution to Diamond Inheritance:
Consider another example, if two teachers teaching same subject to students in a class. Then student may get confused to which type of syntax is used to define a formula or which procedure to be used to solve a problem in a particular subject. But sometimes students will get clear idea about the concept of the subject.
From the above example, two teachers for same subject will be advantage to students with respect to improve their knowledge about the subject. But it will be a problem to them regarding rules of each teacher.
To solve this situation, the management of the school or college keeps two teachers for same subject by labeling main and dummy. One teacher (main) for regular class work and other one (dummy) for doubts clarifications during tuition hours in the school/college. With this idea, student need not follow second teacher rules (dummy) in a subject, but they can clarify their doubts.
In the same way, the diamond Inheritance problem was solved in java by defining a concept of Interface (dummy class). Interface concept will keep the advantage of multiple Inheritance and at the same time it will solve the diamond Inheritance.
An Interface is a dummy class, which contains constants and function prototypes (in complete methods). An Interface is purely in-complete class, nothing is implemented. Therefore we are unable to create an object to an Interface.

Figure 1.3 Diamond Inheritance with Interface

Consider Diamond Inheritance using Interface concept as shown in figure 1.3, in which x is a final constant not a variable like in class. Constant is always constant, even though it is derived into any classes. Therefore confusion with variable in diamond inheritance is avoided by making it as a constant in java using Interface concept and also allows the advantage of multiple inheritance.


Post a Comment


Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

Powered by Blogger