-1

Java does not support multiple inheritance, but in case of interface it does! what does it really mean?

Any example with explanation will be appreciated

thanks!

Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33
  • 7
    I highly advise you to go through basic tutorial to better understand basic things in Java. – Maroun Jun 24 '14 at 08:13
  • Keep in mind that "multiple inheritance" by itself is a bit too broad. There's multiple inheritance of *types*, multiple inheritance of *state*, and multiple inheritance of *implementation*. Java only supports the former (until Java 8, which introduces some of the last one). – awksp Jun 24 '14 at 08:16
  • 1
    have a look at the [diamond problem](http://www.programmerinterview.com/index.php/java-questions/java-diamond-problem/) – earthmover Jun 24 '14 at 08:17
  • You implement an interface, you are not inheriting from it – Ben Jun 24 '14 at 08:24
  • @MarounMaroun to answer this question is not that easy for me while covering basics of Java. because all basic tutorial will covers what interface is and what inheritance is. So, i was unable to answer of this question anywhere. the intent to ask this question is that in one way Java allow us and in other way its not. – Muhammad Suleman Jun 24 '14 at 08:44

5 Answers5

3

In multiple inheritance, child classes inherit both the methods and the data of the parent classes. This can cause problems, especially when the same named entity (whether method or data) can appear in multiple parents, or if two or more parents actually contain the exact same entity they themselves inherited from a common ancestor.

On the other hand, Java's interfaces only declare a set of methods that must be implemented - there's actually no inheritance at all.

And yes, interfaces can of course be grouped into class-like hierarchies, with inheritance. However an implements clause doesn't invoke inheritance, it's just a statement of intent that the given class will (indeed must) contain implementations of every method declared in the interface.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • it may good answer of what multiple inheritance and Java's interfaces are. But my question was why Java doesn't allow to do multiple inheritance and why it provides a way to do that? – Muhammad Suleman Jun 24 '14 at 09:13
  • Java doesn't allow multiple inheritance because in languages that do support it it causes more problems than it resolves. As explained above, specifying multiple interfaces in your `implements` clause is _not_ multiple inheritance _because there is no inheritance_! – Alnitak Jun 24 '14 at 09:16
  • ok so what i have conclude form all answers is that we can't achieve whole features of multiple inheritance using Java interfaces. am i right ? – Muhammad Suleman Jun 24 '14 at 09:19
  • yes, you're right. But then you have to consider what features of multiple inheritance you actually need. Chances are, you're better off with interfaces. – Alnitak Jun 24 '14 at 09:21
  • But don't forget in Java 8 you've got implementation within interfaces, just to make life a bit more complicated. http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html – matt helliwell Jun 24 '14 at 09:55
2

It means an interface can extend multiple interfaces and a class can implement multiple interfaces.

This is not possible in case of classes. A class can extend at most one class.

In reply to your comment

So will we get multiple inheritance features in that way ?

Depends on what do you mean by feature.

Consider a class ChargebleDevices which has device costs (getDeviceCost()) etc and MovableDevices that has if device has movable parts ( getMovableParts()) etc. Now lets say you have a class say Fan and you need it to extend both ChargebleDevices and MovableDevices but that is not possible in java. Instead you can define two interfaces Chargeble and Movable , have abstract methods in it, make your class implement both interfaces and then override method to suit your requirement.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • So will we get multiple inheritance features in that way ? – Muhammad Suleman Jun 24 '14 at 08:15
  • Edited the answer in reply to your comment. – Aniket Thakur Jun 24 '14 at 08:22
  • ok .. but i have one more confusing question in my mind and that is, we can achieve all thing which multiple inheritance provide in that way indirectly then why Java doesn't support it ? i mean in one way Java stops us and in other way it allows us. – Muhammad Suleman Jun 24 '14 at 08:28
  • @sallu interfaces provide _most_, but not _all_ of the features of multiple inheritance. Sometimes you might actually want to inherit the data members of multiple parents, but you can't do that in Java. However it's arguable that concentrating on the methods that are exposed to you and hiding the implementation details can achieve a cleaner design in most cases. – Alnitak Jun 24 '14 at 08:32
  • hmm...! so the intent was to achieve a cleaner design. yes it make sense because until you force people to do this they will not do that most of the time. thanks! – Muhammad Suleman Jun 24 '14 at 08:38
0

Because interfaces specify only what the class is doing, not how it is doing it.

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

Keval Trivedi
  • 1,290
  • 2
  • 13
  • 29
0

Interfaces are like templates and they do not provide any implementation . They are just meant to provide a kind of guideline to the implementing classes. A class may implement any number of intefaces but can extend only one class. The reason for this is that extending two classes that have a same method may create ambiguity when the same method is referred to from the child class (Since it is implemented). But since intefaces do not provide implementation , the implementing class will implement the method , which satisfies the implementation compulsion for both the interfaces . Quoting from the java lanuguage specification

It is permitted for a single method declaration in a class to implement methods of more than one superinterface. For example, in the code:

Example:-

interface Fish { int getNumberOfScales(); }
interface Piano { int getNumberOfScales(); }
class Tuna implements Fish, Piano {

    int getNumberOfScales() { return 91; }
}

the method getNumberOfScales in class Tuna has a name, signature, and return type that matches the method declared in interface Fish and also matches the method declared in interface Piano; it is considered to implement both.

Now we can see this is allowed in java, and whenever we do something like this:-

Tuna t = new Tuna();
t.getNumberOfScales(); 

We know it will always point one function without ambiguity.

Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28
  • What's the difference between a fish and a piano? _You can't tuna fish..._ – Alnitak Jun 24 '14 at 08:42
  • They both are different interfaces, that have a common method signature. Dont get confused, this example is just meant to dictate the scenario when two interfaces have a common method signature and a class implements both interfaces and provides common implementation for the commmon method .And the comment is just a nerdy joke added by sun/oracle people. – Mustafa sabir Jun 24 '14 at 08:50
  • I would also recommend to read this [book](http://www.amazon.com/Head-First-Java-2nd-Edition/dp/0596009208). It covers all basics of java, is fun to read and will answer most of your questions like these. – Mustafa sabir Jun 24 '14 at 08:53
  • thanks for example but i would like to mentioned that i knew what interface is and what inheritance is and how they work. the intent to ask this question is that in one way Java allow us and in other way its not. so what i conclude is that we can't achieve all things using implementing multiple interface which multiple inheritance gives us ? – Muhammad Suleman Jun 24 '14 at 08:57
  • Actually you cannot exactly call multiple interface implementation as multiple inheritance. Inheritance basically means you get all the existing elements of the parent classes. Implementing multiple interfaces just means you fulfilled the contract of each one of them. For example, You cannot simultaneously wear jeans from `armani` and `levis` , that creates ambiguity (synonymous to multiple inheritance). But wearing any jeans might fulfill the contract of many interfaces like `interface LookCool{ WearJeans()}` or `interface officeDressForFridays{ WearJeans()}` etc – Mustafa sabir Jun 24 '14 at 09:51
  • You cannot _remotely_ call "multiple interface implementation as multiple inheritance" because implementation is _not_ inheritance! – Alnitak Jun 24 '14 at 11:23
  • @Alnitak Yes thats exactly what I meant :).Confusion may arise in beginning between abstract class and interface( especially after java 8 introduced the concept of default methods in interface ), but inheritance and interfaces cannot be compared. – Mustafa sabir Jun 24 '14 at 11:54
-1

The important thing to be noted is.. An interface declares the behaviour of any class that implements it. It doesn't provide any behaviour by itself. A Class both "declares" and "provides" behaviour. A class can implement multiple interfaces because it is "providing /defining" its own behaviour corresponding to what the interface which it is implementing declares.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • its a good answer if i have asked difference between Class/Interface, but my question was why Java doesn't allow to do multiple inheritance and why it provides a way to do that? – Muhammad Suleman Jun 24 '14 at 09:02
  • @sallu - If you ahd indeed read the differences between interfaces and classes, you would not have asked this question in the first place.. – TheLostMind Jun 24 '14 at 09:29