Diamond Problem

17,122

Solution 1

Its not the same problem.

In the original problem, the overriden method can be called from A. In your problem this can't be the case because it does not exist.

In the diamond problem, the clash happens if class A calls the method Foo. Normally this is no problem. But in class D you can never know which instance of Foo needs to be called:

         +--------+
         |   A    |
         | Foo    |
         | Bar    |
         +--------+
            /  \
           /    \
          /      \
+--------+        +--------+
|   B    |        |   C    |
| Foo    |        | Foo    |
+--------+        +--------+
          \      /
           \    /
            \  /
         +--------+
         |   D    |
         |        |
         +--------+

In your problem, there is no common ancestor that can call the method. On class D there are two flavors of Foo you can chose from, but at least you know that there are two. And you can make a choice between the two.

+--------+        +--------+
|   B    |        |   C    |
| Foo    |        | Foo    |
+--------+        +--------+
          \      /
           \    /
            \  /
         +--------+
         |   D    |
         |        |
         +--------+

But, as always, you do not need multiple inheritance. You can use aggegration and interfaces to solve all these problems.

Solution 2

In the diamond problem, class D implicitly inherits the virtual method from class A. To call it, class D would call:

A::foo()

If both classes B and C override this method, then the problem comes of which actually gets called.

In your second example however, this isn't the case as class D would need to explicitly state which was being called:

B::foo()
C::foo()

So the problems are not actually the same. In the diamond problem you aren't referencing the derived classes, but their base class, hence the ambiguity.

That's how I understand it, anyway.

Note that I'm coming from a C++ background.

Share:
17,122
cretzel
Author by

cretzel

Java Web Developer

Updated on July 17, 2022

Comments

  • cretzel
    cretzel almost 2 years

    Wikipedia on the diamond problem:

    "... the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C?"

    So the diamond looks like this:

      A
     / \
    B   C
     \ /
      D
    

    My question is, what happens if there is no such class A, but again B and C declare the same method, say foo(). Isn't this the same problem? Why is it then called diamond problem?

    Example:

    class B {
        public void foo() {...}
    }
    
    class C {
        public void foo() {...}
    }
    
    class D extends B, C {
    }
    
    new D().foo();
    
  • cretzel
    cretzel over 14 years
    I don't know C++, but wouldn't it be the same problem if you'd call foo from outside D, say new D().foo()? Then the example without A would also be problematic, right?
  • Toon Krijthe
    Toon Krijthe over 14 years
    @cretzel, yes, in that case you also have a name clash problem.
  • cretzel
    cretzel over 14 years
    The Wikipedia article talks about calling foo() from D, though. And what about calling foo on a D from outside D like new D().foo() (also in my example)?
  • icabod
    icabod over 14 years
    I think in most cases the compiler (for C++ at least) will give an error due to the ambiguity. It has on the compilers I've used, I'm not sure what the C++ standard says about it.
  • Utku
    Utku almost 8 years
    @icabod So, when you write A::foo(), what is called is not the method in class A, but one of the methods in A's children, which causes the ambiguity. But what if you want to call class A's foo()? How do you do that?
  • help-info.de
    help-info.de over 4 years
    There are other answers that provide the OP's question, and they were posted some time ago. When posting an answer see: How do I write a good answer?, please make sure you add either a new solution, or a substantially better explanation, especially when answering older questions. You can comment on old answers instead.