+ 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.
5 Answers
+ 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. āŗ
+ 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šš
+ 6
@Tristel yes it can.. one interface "extends" another interface..
+ 1
follow up question: an Interface can extend more than one other interfaces, can't it?
+ 1
and extension is inheritance, right? So doesn't that mean that java does support multiple inheritance? Just not for classes.