Deadly Diamond of Death


The designers of Java avoided multiplhe inheritance. Replacing it is multiple conformance to interfaces. In Java, there is a structure called an “Interface”. A Java interface is almost identical to a C++ class that has nothing but pure virtual functions. In Java you cannot inherit from more than one base class. However you can “implement” more than one interface.

This UML  diagram shows four classes arranged in the diamond structure that creates the need for virtual inheritance. Both of the classes B and C inherit from class A. D multiply inherits from both B and C. Two problem arise from this. First, which implementation of the ’f’ function does D inherit? Should it inherit B::f() or C::f(). In C++ the answer turns out to be neither. D::f() must be declared and implemented. This eliminates the ambiguity, and certainly this simple rule could have been adopted in Java.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s