1

When i came across the reason why Multiple Inheritance was not included in Java, the reasons given were to keep 'simplicity' and 'reduce complication'. However working with Java environment coming from a C++ background, don't you think that Interface concept to support multi inheritance has complicated the matter rather than solving it? Does it lead to the inference that:

  1. We must NOT use multiple inheritance in Java, and our code architecture should be designed accordingly?
  2. Use concept of Interface for multiple inheritance, which i think is less favorable (atleast for me) compared to the st
jopasserat
  • 5,721
  • 4
  • 31
  • 50
Purushottam
  • 624
  • 1
  • 8
  • 18
  • You can also have multiple inheritance with `abstract` classes. – Johan Sjöberg Oct 18 '11 at 17:22
  • 7
    Is there a question in there than can be answered with anything other than opinion? – Michael Price Oct 18 '11 at 17:22
  • 1
    Possible duplicate http://stackoverflow.com/questions/4897428/java-why-multiple-interfaces-instead-of-multiple-inheritence which is also a duplicate of http://stackoverflow.com/questions/2515477/why-there-is-no-multiple-inheritance-in-java-but-implementing-multiple-interfac and http://stackoverflow.com/questions/3008683/why-does-java-allow-multiple-inheritance-from-interfaces-but-not-from-abstract-c – rhololkeolke Oct 18 '11 at 17:23
  • 1
    Huh? The problems of MI don't exist with interfaces - so how would they complicate the matter? There are a rather small number of cases where MI is the best solution and no better/equivalent pattern would work, but those are rather rare. Not really a question here. – Voo Oct 18 '11 at 17:23
  • 1
    Depends on what they mean by reduce complexity. Do they mean (1) reduce complexity for the developer or (2)reduce complexity for the compiler writer. They achieved (2) not sure about (1) though. – Martin York Oct 18 '11 at 17:31
  • @LokiAstari The rules for MI are more than "a little bit" complex. Could you without reading the exact parts of the standard specify the exact sequence in which classes are searched in arbitrary complex scenarios in c++? Hell look at Python and the mess MI caused there (3 different algorithms because each one had some flaw, a mostly useless super keyword,..). – Voo Oct 18 '11 at 17:34
  • 1
    @Voo: As it happens, yes I can. But in 99.9999% of cases its simple its just complex when you are talking about the general case (which must take into account all the possible variations that are rarely used). But I am not sure what this has to do with my comment above. Java rules make it easier for the compiler writer. Weather it makes it easier for the developers is highly situational and depend on the alternative approaches available for the situation. – Martin York Oct 18 '11 at 17:39
  • @LokiAstari Well Java seems to go for the not unreasonable design idea that anything that would take the developer to understand a dozen pages of the standard to understand a feature isn't such a great idea for the developers either. The same reason why Java also forbids pointer arithmetic - the risks and complexities in most cases outweigh the usefulness. Now people may disagree about what is deemed more useful than problematic, but that's the general idea it seems. – Voo Oct 18 '11 at 17:52
  • @Voo: Don;t see how my statement has anything to do with that. – Martin York Oct 18 '11 at 20:55

4 Answers4

3

You should read Bjarne Stroustrup's point of view about multiple inheritance:

Do we really need multiple inheritance?

Not really. We can do without multiple inheritance by using workarounds, exactly as we can do without single inheritance by using workarounds. We can even do without classes by using workarounds. C is a proof of that contention.

However, every modern language with static type checking and inheritance provides some form of multiple inheritance. In C++, abstract classes often serve as interfaces and a class can have many interfaces. Other languages - often deemed "not MI" - simply has a separate name for their equivalent to a pure abstract class: an interface. The reason languages provide inheritance (both single and multiple) is that language-supported inheritance is typically superior to workarounds (e.g. use of forwarding functions to sub-objects or separately allocated objects) for ease of programming, for detecting logical problems, for maintainability, and often for performance.

quoted from http://www2.research.att.com/~bs/bs_faq2.html#multiple

jopasserat
  • 5,721
  • 4
  • 31
  • 50
  • Well I'm not sure I'd call using pure abstract classes "MI" in the usual sense. You get around all the usual problems of MI that way.. – Voo Oct 18 '11 at 17:27
2

Multiple implementation inheritance and multiple interface inheritance are not the same beasts.

However, it would significantly complicate the GC and other language implementation if they were to add multiple implementation inheritance.

Puppy
  • 144,682
  • 38
  • 256
  • 465
0

I think the choice of Java (and many other OO languages) designers was mainly motivated by the fragile base problem. It's true we don't need multiple inheritance, but for that's worth to note we don't need single either. Object Oriented programming it's about identities of entities. Inheritance can be seen as syntax sugar in this regard.

CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • 1
    If the fragile base problem was motivation, I wonder why they left all classes non-final and all methods virtual by default. – R. Martinho Fernandes Oct 18 '11 at 17:32
  • The difference is that single inheritance is relatively easy to implement, offers many useful advantages and no real disadvantages. MI is much more controversial in those regards. – Voo Oct 18 '11 at 17:35
-1

The interface concept in java is NOT meant for providing multiple inheritance feature.

Sri
  • 568
  • 1
  • 6
  • 18
  • 1
    "The Java programming language does not permit multiple inheritance (inheritance is discussed later in this lesson), but interfaces provide an alternative." quoted from http://download.oracle.com/javase/tutorial/java/IandI/createinterface.html – jopasserat Oct 18 '11 at 17:25
  • @Srikaantha I strongly disagree. The ONLY time you ever need an interface is when you need the benefits of multiple inheritance. It has no more functionality than an ordinary "extends", the ONLY thing it provides is the ability to implement MORE THAN ONE of them, hence "multiple" inheritance, albeit of interface, not implementation. An interface has no other use. – Navigateur Nov 28 '11 at 15:55