Can the Diamond Problem be really solved?
Posted
by Mecki
on Stack Overflow
See other posts from Stack Overflow
or by Mecki
Published on 2009-02-18T16:04:43Z
Indexed on
2010/04/26
7:53 UTC
Read the original article
Hit count: 410
A typical problem in OO programming is the diamond problem. I have parent class A with two sub-classes B and C. A has an abstract method, B and C implement it. Now I have a sub-class D, that inherits of B and C. The diamond problem is now, what implementation shall D use, the one of B or the one of C?
People claim Java knows no diamond problem. I can only have multiple inheritance with interfaces and since they have no implementation, I have no diamond problem. Is this really true? I don't think so. See below:
[removed vehicle example]
Is a diamond problem always the cause of bad class design and something neither programmer nor compiler needs to solve, because it shouldn't exist in the first place?
Update: Maybe my example was poorly chosen.
See this image
Of course you can make Person virtual in C++ and thus you will only have one instance of person in memory, but the real problem persists IMHO. How would you implement getDepartment() for GradTeachingFellow? Consider, he might be student in one department and teach in another one. So you can either return one department or the other one; there is no perfect solution to the problem and the fact that no implementation might be inherited (e.g. Student and Teacher could both be interfaces) doesn't seem to solve the problem to me.
© Stack Overflow or respective owner