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?

9
  • @org, note that the one you found is itself a duplicate - I linked in the original :-) Commented Feb 4, 2011 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. Commented Feb 4, 2011 at 11:28
  • @Péter @Any Moderator it it should be automatically put the concrete duplicate here Commented Feb 4, 2011 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?". Commented Feb 4, 2011 at 11:34
  • 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. Commented Feb 4, 2011 at 11:58

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

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 {}
edit "cyclic inheritance" -> "diamond problem". used a wrong term indeed. inconvenience regretted..!!! thanks for spotting it, +1 for your comment!
How is the diamond a "problem"?
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

1 Comment

There is no "diamond problem". (And you can have a "diamond" with interfaces too.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.