How inheritance differs from Java to C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How inheritance differs from Java to C++?

My question is mainly concerned with multiple inheritance, I know that Java forbids it, but ehat about C++ and why??

6th Sep 2018, 3:07 AM
Loaie Amgad
Loaie Amgad - avatar
5 Answers
+ 6
As you said C++ allows multiple inheritance, which has led to some situations with very buggy code when used too liberally. The designers of Java wanted to prevent this so you can only extend one class (but implement multiple interfaces) However some complain that this isn't available in Java, and done carefully it can be useful. The lines are a bit blurred now that Java 8+ has default methods for interfaces, which really does start to look like multiple inheritance
6th Sep 2018, 3:17 AM
Dan Walker
Dan Walker - avatar
+ 4
The reason that java disable multiple inheritance with extend keyword to prevent collision between methods with same names of different classes. For example there is a class called A and B there is a method called justAMethod() in both of them. So JVM has to think about which method to perform.
7th Sep 2018, 3:11 PM
Seniru
Seniru - avatar
+ 3
With any language it is possible to produce poor quality, buggy code. That's why we have design principles to help us utilise the features of a language to the best effect. There are probably things that can be implemented much easier in C++. It's a very powerful language and is still used lots today and is in fact capable of things that Java cannot do, like manipulating memory
6th Sep 2018, 3:30 AM
Dan Walker
Dan Walker - avatar
+ 1
I use multiple inheritance in "flavor" style. It means that there is multiple helper class (usually template), that implement specific behavior or feature. For example: linking in list, sending signals, subscribing and receiving signals. When I design new class I combine required functional by inheriting such classes.
6th Sep 2018, 5:56 AM
Sergey Ushakov
Sergey Ushakov - avatar
0
Does that make C++ a buggy old language that must be forgot???
6th Sep 2018, 3:20 AM
Loaie Amgad
Loaie Amgad - avatar