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