5

Recently ,after reading some articles in Programming Languages and Practice book ,it is mentioned that multiple interface inheritance in Java does not suffer from the same problems as multiple class inheritance in C++.

But I can't understand why this happens. How java is able to use multiple interface inheritance while in C++ implementation errors exists ??

Is there a way to replace multiple inheritance in C++ as to avoid implementation problems ??

For the last statement to be more specific ,lets say that we have:

class A {...};
class B : public A {...};
class C : public A {...};
class D : public B, public C {...};

Then class D inherits class B,C which both inherit class A. So if A had a field-variable then B,C would have the same variable name ,then what variable would D has (inherited from B or C) .To avoid this could we write the above code without multiple inheritance but with similar results?

The question is not a duplicate since it is not focused on what finally will be the inheritance in the example ,but to understand the difference between Java-C++ multiple inheritance(see the first question above) and also if there is a way suggested to overcome some multiple inheritance problems (like the above).

coder
  • 12,832
  • 5
  • 39
  • 53
  • 1
    Isn't Java single-inheritance? –  Sep 08 '16 at 21:18
  • Possible duplicate of [Implementing two interfaces in a class with same method. Which interface method is overridden?](http://stackoverflow.com/questions/2801878/implementing-two-interfaces-in-a-class-with-same-method-which-interface-method) – acm Sep 08 '16 at 21:19
  • Why do you put space before a comma in text ,and not the other way around? – LogicStuff Sep 08 '16 at 21:19
  • @Tobias ,from what i read it is single-inheritance for classes but not for interfaces... – coder Sep 08 '16 at 21:19
  • Interfaces cannot have data members or non-pure-virtual methods though, so your scenario simply cannot happen in Java. – Miles Budnek Sep 08 '16 at 21:21
  • 1
    you cannot have the diamond effect with data members since interfaces don't allow data members, only abstract methods. – Jean-François Fabre Sep 08 '16 at 21:21
  • Your example perfectly describes the diamond problem : http://www.programmerinterview.com/index.php/c-cplusplus/diamond-problem/ – Frank Bessou Sep 08 '16 at 21:23
  • @Frank Bessou ,it is just an example where we have a problem ,I'm not focused in that but mostly to understand the first question-statement...and if is another way without using multiple inheritance to avoid problems but keeping some advantages of multiple inheritance... – coder Sep 08 '16 at 21:25

4 Answers4

5

Java (unlike C++) does not allow multiple inheritance of state and, therefore, does not suffer from a diamond problem.

It allows multiple inheritance of type through interfaces (a class can implement multiple interfaces).

Starting with Java 8 there is also multiple inheritance of behavior through default methods in interfaces.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • This replies only partially to the question. The OP was asking for _alternatives_ to the multiple inheritance, if possible. – skypjack Sep 08 '16 at 21:33
3

But I can't understand why this happens. How java is able to use multiple interface inheritance while in C++ implementation errors exists???

That is possible because Java does not allow multiple inheritance, but only multiple implementation from multiple interface.

Implementation is different than inheritance.

The general problem with multiple inheritance is (in short) that two classes may define different ways of doing the same thing, and the subclass can't choose which one to pick.

Since interface in java can only declare the signature of methods without implementing them, the problem does not exists if multiple interface are derived.

In conclusion, in order to avoid the problem Java forbids directly multiple inheritance, and allows only multiple implementation of interface.


Is there a way to replace multiple inheritance in c++ as to avoid implementation problems???

The first suggest I can give you is to avoid a design so complex.

Anyway the problem you've exposed in your question is very common and known as diamond problem.

C++ provides a solution in order to solve that problem with the usage of virtual inheritance.

Let an inheritance graph:

    A
 /     \
B       C
 \     /
    D

This is the problem:

C++ by default follows each inheritance path separately, so a D object would actually contain two separate A objects, and uses of A's members have to be properly qualified.

And this is a briefly explanation about the virtual inheritance:

If the inheritance from A to B and the inheritance from A to C are both marked "virtual" (for example, "class B : virtual public A"), C++ takes special care to only create one A object, and uses of A's members work correctly. If virtual inheritance and nonvirtual inheritance are mixed, there is a single virtual A and a nonvirtual A for each nonvirtual inheritance path to A.

In short, with virtual inheritance you prevent the part class A is duplicate in the class D.

BiagioF
  • 9,368
  • 2
  • 26
  • 50
1

To avoid this could we write the above code without multiple inheritance but with similar results?

It follows a minimal, working example:

struct A {};
struct B {};
struct C {};

struct D {
    operator A&() { return a; }
    operator B&() { return b; }
    operator C&() { return c; }

private:
    A a;
    B b;
    C c;
};

void f(const B &b) {}

int main() {
    D d;
    f(d);
}

If you don't want to create an explicit hierarchy, but still you would like to use an instance of D where a reference to B is required, the solution above works just well.
It is based on composition instead of inheritance, as you can see.
It has some limits, of course, as an example you cannot cast a pointer to D to a pointer to B.

If it fits your requirements mostly depends on the real problem, so I cannot say.
Anyway, that's is a viable solution somehow similar to the inner class idiom, if you are interested in having further details.

skypjack
  • 49,335
  • 19
  • 95
  • 187
0

The "multiple inheritance" in Java is no real multiple inheritance. Since Java can only create the diamond problem by using interfaces.

Interfaces are only a reference to an implemented method in the sub class, because an interface cannot implement a method.

So the child class cannot have parents with different implementions. It doesn't matter from which interface you inherit the method, since no implementation exists.

Edit: oh I forgot how Java destroyed the principle behind interfaces by introducing default methods...

Jhonny007
  • 1,698
  • 1
  • 13
  • 33
  • Thanks a lot for the answer !! – coder Sep 08 '16 at 21:46
  • Java cannot create the diamond problem. There will be only one implementation of a given method. – user4581301 Sep 08 '16 at 22:41
  • Actually with default methods you can create the problem: http://stackoverflow.com/questions/16764791/how-does-java-8-new-default-interface-model-works-incl-diamond-multiple-inhe there are multiple reasons why default methods are bad and this isn't even the worst. I think the intend was good, the implementation was terrible. – Jhonny007 Sep 08 '16 at 22:54