+ 3

Can Java support multiple inheritance?

13th Sep 2017, 2:29 AM
Maduchandan B P
7 odpowiedzi
+ 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.
7th Oct 2017, 9:35 AM
Amir Masoud Mostofinejad
Amir Masoud Mostofinejad - avatar
+ 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)
13th Sep 2017, 2:32 AM
Rrestoring faith
Rrestoring faith - avatar
+ 8
No
13th Sep 2017, 2:31 AM
P R
P R - avatar
+ 3
No, a class can extend at most one parent class. However a class can implement more than one interface.
13th Sep 2017, 3:55 AM
Eric Blinkidu
Eric Blinkidu - avatar
+ 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
7th Oct 2017, 2:12 PM
Amir Masoud Mostofinejad
Amir Masoud Mostofinejad - avatar
0
And as of Java 7 you can actually put code in interfaces. So yeah, you do have multiple inheritance.
13th Sep 2017, 5:11 AM
1of3
1of3 - avatar
0
I repeat, as of Java 7 you can put implementation in interfaces. Get up to date.
7th Oct 2017, 1:48 PM
1of3
1of3 - avatar