7

Possible Duplicate:
Why there is no multiple inheritance in Java, but implementing multiple interfaces is allowed

hi all, I am new to java and when I am try to use multiple inheritance concept in java its showing a compile time error.Why java does not support it.I heard about interface key word but do not know why java support it directly. please help me in this and explain.

Community
  • 1
  • 1
Subhransu Mishra
  • 3,035
  • 11
  • 40
  • 47
  • 1
    http://stackoverflow.com/questions/995255/why-is-multiple-inheritance-not-allowed-in-java-or-c http://stackoverflow.com/questions/2515477/why-there-is-no-multiple-inheritance-in-java-but-implementing-multiple-interface http://stackoverflow.com/questions/3008683/why-does-java-allow-multiple-inheritance-from-interfaces-but-not-from-abstract-co http://stackoverflow.com/questions/1262447/multiple-inheritance-in-java http://stackoverflow.com/questions/1038314/alternative-of-multiple-inheritance-in-java – tim_yates Sep 22 '10 at 09:56

3 Answers3

21

The main problem with multiple inheritance (alluded to by Colin and Rin) is known is The Diamond Problem.

I quote:

The diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C?

AlcubierreDrive
  • 3,654
  • 2
  • 29
  • 45
16

Multiple inheritance can be really difficult to understand. When you have a multiple inheritance with two classes which have methods in conflicts, how do you handle this ?

Of course solutions exist (in C++ for example) but the creators of Java decided it was way to complicated and not really in the Java philosophy (make development a lot easier).

From sun.com :

Multiple inheritance--and all the problems it generates--was discarded from Java. The desirable features of multiple inheritance are provided by interfaces--conceptually similar to Objective C protocols.
An interface is not a definition of a class. Rather, it's a definition of a set of methods that one or more classes will implement. An important issue of interfaces is that they declare only methods and constants. Variables may not be defined in interfaces.


Resources :

Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
1

Because, it was hard to use it. Instead, Java has interface what is much better solutions.

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66