Converting C++ Multiple Inhertance Hierarchies Java Help

Converting C++ Multiple Inheritance Hierarchies

In both cases, subclass C inherits classes A and U. However, in the hierarchy on the left, C Inherits both A and B at the same time. In the one on the right, Inherits A,  B. By not allowing the inheritance of multiple base classes by a single subclass, Java Safety simplifies the Inheritance model. Multiple inheritance carrels with it several special cases that must be handled. This adds overhead to both the compiler and the run-time system, while providing only marginal benefit for the programmer. Since C++ supports multiple inheritance and Java does not, you may have to deal with this issue when porting C++ application to Java. While every situation is , different, two general pieces of advice can be offered. First, in many cases, multiple inheritance is employed in n C++ program when there is actually no need to do ,so. When this is the case, just convert the class structure to a single-inheritance hierarchy. For example, consider this C++ class hierarchy that defines a class called House:

Notice that House multiply inherits Foundation, Walls, and Rooms. A leatherette is
nothing wrong with structuring a C++ hierarchy like this, it is not necessary. For example, here is the same set of classes structured for Java:

Here, each class extends the preceding one, with House becoming the final extension. Sometimes a multiple inheritance hierarchy is more readily converted by including objects of the multiply inherited classes in the “final object-For example, here is another way that House could be constructed in Java.Here, Foundation, Walls, and Rooms are objects that are part of House rather than inherited by House.
One other point: sometimes a c++ program will contain a multiple-inheritance hierarchy simply because of poor initial design. A good time to correct this type of design flaw i~when you port to Java.

  • Feel free to send us an inquiry, we will reply back in hours.


Posted on September 18, 2014 in Migrating from C+tto Java

Share the Story

About the Author

Back to Top