+ 4

About java multiple Inheritance.

There are question in java challange that 1.Do java support multiple inheritance? answer:No 2:What can be used to achieve multiple inheritance in java? answer: Interfaces I know a class should have only one superclass..so extending from two classes is not possible in java. But implementing from two Interface is possible. So multiple inheritance is possible in case of Interface. Any thoughts on challange question.

27th Jan 2018, 12:01 PM
Sunil Thakali
Sunil Thakali - avatar
5 Réponses
+ 7
java is all about class and objects. interfaces are not classes. interfaces defines what behavior the implementing class will have but not how. you HAVE to override the methods in the class (one that implements interface) and thus you WILL need a class to do so, and classes do not support multiple inheritance. you can achieve somewhat like Multiple Inheritance by the point you said @Tristel, but with limitations and drawbacks. it's indirectly possible (to some extend) but directly is not. ☺
28th Jan 2018, 6:40 PM
Debsmita Paul
Debsmita Paul - avatar
+ 6
you can achieve multiple inheritance in java by using interface - its partially true. you can implement multiple interfaces and use its methods but to a limited extend. an interface doesn't act like a class, as it includes blank methods which needs to be overridden in the class which implements the interface (unless it is an abstract class). now imagine if you have a load of blank methods in two or more interface and you implement all the interfaces in one class then you have to override all the methods (even if you don't need them). which is not desired and also not the case with inheriting classes. so although you can achieve multiple inheritance using interface but there are limitations. though I think that in jdk 1.8 they have included something like "default methods" in interfaces which will determine how the methods, which are not overridden in the implementing class, will work (something like that I'm not sure though). but the base concept with interface remains same. hope this helps☺👍 please don't hate if I'm wrong.. I'm still learning😇🙏
27th Jan 2018, 1:46 PM
Debsmita Paul
Debsmita Paul - avatar
+ 6
@Tristel yes it can.. one interface "extends" another interface..
28th Jan 2018, 6:31 PM
Debsmita Paul
Debsmita Paul - avatar
+ 1
follow up question: an Interface can extend more than one other interfaces, can't it?
28th Jan 2018, 6:29 PM
Tristel
Tristel - avatar
+ 1
and extension is inheritance, right? So doesn't that mean that java does support multiple inheritance? Just not for classes.
28th Jan 2018, 6:33 PM
Tristel
Tristel - avatar