82
votes
Accepted
Why is "diamond problem" a problem? Why doesn't the child simply call both parent's methods sequentially? Why is a thing with solutions a problem?
One problem with your approach is that it only works if the method f() has no return value. What should happen if it returns some value? Should it only return the second one?
With variables you have ...
37
votes
Why is "diamond problem" a problem? Why doesn't the child simply call both parent's methods sequentially? Why is a thing with solutions a problem?
It is very, very, very unlikely that I want to call both methods.
Imagine I inherit from three classes, Gunfighter, Chessgame, and Artist, and each has a Draw() method. The best outcome would be ...
23
votes
Should a trait refer to parent methods?
It's fine if a trait depends on methods in the class into which it is embedded. But these dependencies should be explicit, by declaring an abstract function. PHP can then check that such a method ...
16
votes
Why is "diamond problem" a problem? Why doesn't the child simply call both parent's methods sequentially? Why is a thing with solutions a problem?
Your proposed solution only works for void functions, and only when the side-effects of them can safely compose. You still have to choose which value to return.
class A{
};
class B : public A{
public:
...
13
votes
Access methods from two interfaces's implementation in a Class
It's perfectly possible for you to have
a[n] abstract class AbstractCache that implements these 2 interfaces and then this Cache class will extend that abstract class
public interface UserCache {
...
12
votes
Why is "diamond problem" a problem? Why doesn't the child simply call both parent's methods sequentially? Why is a thing with solutions a problem?
What is the "ambiguous" thing here? Why must I assume d->f() would logically call A's f() twice at here?
Your question is actually just a reword of why is diamond inheritance a problem, ...
10
votes
Avoiding vtable pointers in objects in C++
How about instead, making a pointer "two pointers".
This would bloat every pointer, even those which don't use dynamic polymorphism. Remember: you have to be able to cast any object pointer to void* ...
8
votes
Should a trait refer to parent methods?
Yes, it's a code smell. If a trait is used to make code more portable, then then this sort of thing makes it less portable. You shouldn't do it. Without having a more concrete example it looks like ...
7
votes
Why is "diamond problem" a problem? Why doesn't the child simply call both parent's methods sequentially? Why is a thing with solutions a problem?
Is it a problem?
The problem with the diamond and multiple inheritance is the ambiguity. Not in the syntax, but in the semantics. You just have to decide what you want.
In your code, there are two A ...
6
votes
share method logic along classes without inheriting from abstract class
To make sure all classes which have hierarchical components do this I
created an abstract class which they all inherit so no one forgets to
do it both ways.
An interface would also "remind" ...
6
votes
Accepted
Why doesn't java allow multiple inheritance of classes when it allows multiple inheritance of interfaces?
Partially, this is historical. It would be much more difficult to add multiple inheritance now. For example, the Class.getSuperclass() method would no longer make sense, as a class might have multiple ...
5
votes
Why is "diamond problem" a problem? Why doesn't the child simply call both parent's methods sequentially? Why is a thing with solutions a problem?
The diamond problem is a problem because there is no one obvious answer to what is resolved. That is, your solution might work, but you can't assume that everyone else will think that's the obvious (...
5
votes
Accepted
What is the use of Multiple Inheritance in languages like C++ and Python?
While one could justify multiple inheritance via appeals to some philosophical aspects of OOP (for example, pointing out that there are a multitude of devices that can legitimately be considered both &...
5
votes
Accepted
Interfaces in Python, multiple inheritance vs. a home-made solution
Using Python's multiple inheritance (MI) for interfaces, abstract base classes, mixins, or similar techniques is perfectly fine. In most cases, the MRO produces intuitive results.
However, object ...
4
votes
Why doesn't java allow multiple inheritance of classes when it allows multiple inheritance of interfaces?
As mentioned in other answers, the main reason is the diamond inheritance problem. Prior to Java 8, there were no default interfaces so this issue did not exist in Java at all and this was completely ...
4
votes
Why doesn't java allow multiple inheritance of classes when it allows multiple inheritance of interfaces?
You are asking the wrong question. It's not "why can't Java", it's "why doesn't Java". Answer: Multiple inheritance is a pain in the arse. Most C++ code doesn't use multiple inheritance. Two highly ...
4
votes
Avoiding vtable pointers in objects in C++
How about instead, making a pointer "two pointers".
Yes, I think that could be done, theoretically — there are a lot of ways C++ could be implemented. These choices are all trade-offs of one ...
4
votes
Decorate class which inherits from concrete class(es)
If applying design patterns, avoid shortcuts
Your decorator is an incomplete one:
The inheritance to the decorated class (Component in GoF terminology) is not for reuse its implementation, but to ...
3
votes
Avoiding vtable pointers in objects in C++
How about instead, making a pointer "two pointers". The first pointer is a pointer to the object. The second is a pointer to it's vtable.
This is almost exactly how Go implements polymorphism via ...
3
votes
Accepted
"Subtractive" behaviour extension vs overriding a function completely - what are the pros/cons?
If you work with vendor code, you don't RY because it wasn't you who wrote the original code :-) So don't make your life harder than it needs to be.
Regarding the subtractive extension vs. keeping a ...
3
votes
Accepted
Correct way to extend a hierarchy tree
The problem here is that you've been mislead to express these kind of relationships with inheritance. You have not done anything wrong, this problem typifies the problems with inheritance and why it ...
2
votes
Correct way to extend a hierarchy tree
The fact that you have created several classes whose functionality is sometimes mutually exclusive (Buyer vs. Seller) and sometimes cumulative (Admin) proves that (when working with Java) these cannot ...
2
votes
Accepted
Client to application interaction results in fat interfaces
When looking at fat interfaces like you have there, the challenge is to find the right abstraction that will allow you to break it up in reasonable chunks. The approach I look at goes something like ...
2
votes
Interfaces in Python, multiple inheritance vs. a home-made solution
In order to ensure a class has some properties, I make base "interface" classes
While this is a common design pattern in statically typed languages, Python programmers consider more idiomatic to use ...
1
vote
Select inheritance hierachy at run time
To answer the question directly, types in Python are first-class citizens, which means you can do the following:
Machine11_base = find_base_class("machine_11")
class Machine11(Machine11_base)...
1
vote
Selective method inheritance
Your goal
You have contradicting expectations.
On the other hand, not all microservices support all the methods and I wouldn't want the classes to have methods that are not supported.
In other words,...
1
vote
Accepted
Using virtual inheritance for an interface system based on abstract classes
Your design challenge
On one side, you have an abstraction IShape, and more specialized abstractions like IShapeConvex, IShapeHullConvex. On the other side, you have implementations of these ...
1
vote
Accepted
Approach for implementing Device and Protocol layers in C++?
Main problem with your first approach, as you discovered correctly already, is that you create a diamond pattern:
class A { };
class B : public A { };
class C : public A { };
class D : public B, ...
1
vote
Separated implementations or one with all functionalities
Think functional, not object-oriented. Functional techniques provide a work-around to multiple inheritance. Your interface/implementation pairs are essentially the same as first class functions.
...
1
vote
Accepted
What would be the best option in this scenario?
As you're pointing out, the inheritance solution requires introduction of hierarchy that serves very little value. In particular the use of multiple inheritance because someone is both a teacher and ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
multiple-inheritance × 53inheritance × 22
object-oriented × 15
c++ × 12
java × 9
interfaces × 7
python × 6
c# × 5
mixins × 5
design × 4
design-patterns × 4
scala × 3
composition × 3
architecture × 2
php × 2
object-oriented-design × 2
programming-languages × 2
coding-style × 2
naming × 2
class-design × 2
single-responsibility × 2
class-diagram × 2
generics × 2
trait × 2
javascript × 1