-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!

5
  • 7
    I highly advise you to go through basic tutorial to better understand basic things in Java. Commented Jun 24, 2014 at 8: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). Commented Jun 24, 2014 at 8:16
  • 1
    have a look at the diamond problem Commented Jun 24, 2014 at 8:17
  • You implement an interface, you are not inheriting from it Commented Jun 24, 2014 at 8: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. Commented Jun 24, 2014 at 8:44

5 Answers 5

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.

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

5 Comments

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?
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!
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 ?
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.
But don't forget in Java 8 you've got implementation within interfaces, just to make life a bit more complicated. docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
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.

5 Comments

So will we get multiple inheritance features in that way ?
Edited the answer in reply to your comment.
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.
@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.
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!
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.

7 Comments

What's the difference between a fish and a piano? You can't tuna fish...
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.
I would also recommend to read this book. It covers all basics of java, is fun to read and will answer most of your questions like these.
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 ?
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
|
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.

2 Comments

i knew the problems of multiple inheritance but my question is that in one way we can't do and in other way we can .. why is that ?
with the help of interface we can solve the diamond problem.
-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.

2 Comments

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?
@sallu - If you ahd indeed read the differences between interfaces and classes, you would not have asked this question in the first place..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.