+ 3
Can Java support multiple inheritance?
7 Réponses
+ 1
In c++ we have multiple inheritance. and that causes much complexity. so in java they have decided to limit the inheritance number to 1. and to deal with it requires different kind of thinking and design diagrams. but however for polymorphism usages, they made the number of classes that one class can implement, limitless. but the point is that this doesn't add complexity to the code like multiple inheritance. and the reason is that if you have class a and class b that they both have the print method, and imagine you have class c that extends both a and b, now if you call the print method on c, you have to specifically say which of a and b classes print method you want to use. and if you have more than one layer of inheritance, this will make coding super complex.
But in multiple implementations, all of the classes that you implement must be interfaces, which means they have abstract methods that dont have any code in them and they all must be overrided. so int the example above, imagine you have interface a and interface b which class c implements both, and if interface a and b both have print method, you will have to implement only one print method on class c, and whenever you call c's print method, there will be only one code to run.
+ 10
If you include interfaces, sure, Java supports that.
But generally when you say 'multiple inheritance', it is referring to classes. That is what Java doesn't support. (Inheriting more than one class)
+ 8
No
+ 3
No, a class can extend at most one parent class. However a class can implement more than one interface.
+ 1
you are correct sir. java supports codes and implementation in interfaces in its last updates. but the fact is, that if you have some print method in both a and b interface, and you implement them both in class c, you must override the print method otherwise you'll get compile error
0
And as of Java 7 you can actually put code in interfaces. So yeah, you do have multiple inheritance.
0
I repeat, as of Java 7 you can put implementation in interfaces. Get up to date.