1

Possible Duplicates:
Why does Java allow multiple inheritance from interfaces but not from abstract/concrete classes
Why there is no multiple inheritance in Java, but implementing multiple interfaces is allowed

Instead of inheriting from multiple classes (which Java doesn't allow), why are we told to implement multiple interfaces instead?

Surely the point of inheriting from multiple classes is the inherit their functionality - if you have to manually re-insert functionality (for each class extending a set of interfaces) what's the point of using the interfaces? There's no guarantee that two classes implementing the same set of interfaces will provide the same functionality - or am I missing something?

Community
  • 1
  • 1
Michael
  • 7,348
  • 10
  • 49
  • 86
  • @org, note that the one you found is itself a duplicate - I linked in the original :-) – Péter Török Feb 04 '11 at 11:26
  • There **is** a guarantee that two classes implementing the same set of interfaces will provide the same functionality: that's what interfaces are for. – R. Martinho Fernandes Feb 04 '11 at 11:28
  • @Péter @Any Moderator it it should be automatically put the concrete duplicate here – jmj Feb 04 '11 at 11:33
  • My question isn't why can't I extend multiple classes. It's "What is the point of using multiple interfaces instead?". – Michael Feb 04 '11 at 11:34
  • Two classes that extend the same interfaces might provide the same methods / return types - but one implementation may be buggy, the other not - just because they implement from the same interfaces, there's no guarantee of anything. Why is this the preferred approach - why don't they just provide the functionality stand-alone? – Michael Feb 04 '11 at 11:43
  • 1
    @Mikaveli: if the implementation is buggy, why are you using it? If you want guarantees that two classes derived from a common base (be it a class or an interface) have the same implementation, polymorphism (be it with interfaces or classes) is the wrong tool for the job. Even with multiple class inheritance you have no guarantees that the base classes don't override those implementations. – R. Martinho Fernandes Feb 04 '11 at 11:58
  • I see 2 questions here. Of course 2 classes implementing an interface do their own implementation, which might vary. So their behavior isn't guaranteed to be identical. Yes, bad experiences with the diamond pattern exists, but scala, which produces bytecode for the JVM uses multiple inheritance in form of traits. You can use a delegate, to do the job of 2 classes, implementing the same interface. – user unknown Mar 08 '11 at 14:02
  • @user unknown: Thank you, I think a 'delegate' class is really what I've been looking for. – Michael Mar 08 '11 at 14:48
  • If you edit your question, I might give the answer formally as such. – user unknown Mar 08 '11 at 16:36

3 Answers3

1

Multiple inheritance is something that can cause multiple problems.

Interfaces are used to give abilities to instances of the class that implement them. I personally use interfaces with composition(using instance variables that are references to other objects) in order to provide functionality to my class that would otherwise be achieved with multiple inheritance.

In other words my class provides the functionality promised by the interface implemented but internally my class instance uses the instance variable to do the job.

"There's no guarantee that two classes implementing the same set of interfaces will provide the same functionality - or am I missing something?"

About your statement above:

Each method should adhere to a contract so no matter how you implement it the functionality of the method should always be the same if this is what is supposed to do. If it breaks the contract it means it was implemented wrongly.

Timmo
  • 3,142
  • 4
  • 26
  • 43
0

multiple inheritance may lead to cyclic inheritance.. to avoid that we are going for interface based inheritance..

R K
  • 1,283
  • 1
  • 14
  • 41
  • 2
    Care to explain what you mean by "multiple inheritance may lead to cyclic inheritance"? Single inheritance does as well: `class Foo extends Bar {} class Bar extends Foo {}` – R. Martinho Fernandes Feb 04 '11 at 11:33
  • edit "cyclic inheritance" -> "diamond problem". used a wrong term indeed. inconvenience regretted..!!! thanks for spotting it, +1 for your comment! – R K Feb 05 '11 at 06:44
  • How is the diamond a "problem"? – curiousguy May 22 '13 at 07:01
0

You should read about the diamond dependency problem http://en.wikipedia.org/wiki/Diamond_problem and to avoid this Java chose interfaces over extension of multiple classes

Manoj
  • 5,542
  • 9
  • 54
  • 80