Converting C++ Multiple-Inheritance Hierarchies to Java

By: Daniel Malcolm Emailed: 1768 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

C++ allows one class to inherit two or more base classes at the same time. Java does not. To understand the difference, consider the two hierarchies depicted here: In both cases, subclass C inherits classes A and B. However, in the hierarchy on the left, C inherits both A and B at the same time. In the one on the right, B inherits A, and C inherits B. By not allowing the inheritance of multiple base classes by a single subclass, Java greatly simplifies the inheritance model. Multiple inheritance carries 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++ applications to Java. While every situation is different, two general pieces of advice can be offered. First, in many cases, multiple inheritance is employed in a 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:

class Foundation {
// ...
class Walls {
// ...
class Rooms {
// ...
class House : public Foundation, Walls, Rooms {
// ...

Notice that House multiply inherits Foundation, Walls, and Rooms. While there 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:

class Foundation {
// ...
class Walls extends Foundation {
// ...
class Rooms extends Walls {
// ...
class House extends Rooms {
// ...

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:

class Foundation {
// ...
class Walls{
// ...
class Rooms {
// ...
/* Now, House includes Foundation, Walls, and Rooms
as object members.
class House {
Foundation f;
Walls w;
Rooms r;
// ...

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 is when you port to Java.

C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


Be the first one to add a comment

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Daniel Malcolm
javac options in Java
Operator Precedence in Java
Calling Multiple Listeners in JSF
Using free() Function in C
ForwardAction in Struts
Listing Files and Directories sample program in C
Binary Tree - (Self-referential Structures) example program in C
A simple program using EL in JSP
Command-line Arguments in C
Example Calculator program in C - describing use of External Variables in C
Assignment Operators and Expressions in C
The for statement in C
JSF Basics
assert() Versus Exceptions in C++
RMS Basics in J2ME

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Using Comments in a C++ Program
do...while Loops in C++
Using switch Statements in C++
The Stack and the Free Store in C++
Converting Pointer Parameters in C++ to Java
Converting C++ Multiple-Inheritance Hierarchies to Java
Operator Precedence in C++
assert() example program in C++
Getting User Input Using cin in C++
Implementing Pure Virtual Functions in C++
Vector variables in C++
Sum of 1 to N in C++
Multi-Line Literals in C++
Enumerations in C++
Calculating total based on the given quantity and price in C++
Most Emailed Articles (in C++)
Demonstrating global and local variables in C++
C++ and Object-Oriented Programming
Using cout in C++
Use of Conditional (Ternary) Operator in C++
const Member Functions in C++
Classes with Other Classes as Member Data in C++
What Is a Pointer in C++?
Manipulating Data by Using Pointers
Stray or Dangling Pointers in C++
C++ Destructors Versus Java Finalization
atoi(), itoa() in C++
strcat() and strncat() sample program in C++
Using #define, The Preprocessor and the Compiler in C++
Getting User Input Using cin in C++
Pass by Reference in C++ functions