Converting C++ Multiple-Inheritance Hierarchies to Java

By: Daniel Malcolm Emailed: 1696 times Printed: 2203 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++ )
ctime() sample program in C++
Difference between Procedural, Structured, and Object-Oriented Programming
Using Comments in a C++ Program
C++ Recursion function explained using Fibonacci series
Public versus Private members in C++
Constructors and Destructors in C++
Classes with Other Classes as Member Data in C++
Multiple statements in for loops in C++
Nested Loops in C++
The indirection operator (*) - dereference operator.
this Pointer in C++
Converting C++ Multiple-Inheritance Hierarchies to Java
strcpy() and strncpy() sample program in C++
virtual inheritance example in C++
Checking Prime Number in C++
Most Emailed Articles (in C++)
Using cout.width() in C++
qsort() sample program in C++
Reverse a String in C++
C++ and Object-Oriented Programming
Using cout in C++
Demonstration of Prefix and Postfix operators in C++
Use of Conditional (Ternary) Operator in C++
What Is a Pointer in C++?
The Stack and the Free Store in C++
this Pointer in C++
How to handle Exceptions in C++
Using #define, The Preprocessor and the Compiler in C++
assert() Versus Exceptions in C++
cin.ignore() in C++
Opening files for read and write in C++