Java Reference
In-Depth Information
Figure 9.8. The diamond problem
Now what happens if B also has a default hello method with the same signature? Rule 2 says
that you select the most specific default-providing interface. Because B is more specific than A,
the default method declaration from B will be selected. If both B and C declare a hello method
with the same signature, you have a conflict and need to solve it explicitly, as we showed earlier.
Just as a side note, you may be wondering what happens if you add an abstract hello method
(one that's not default) in interface C as follows (still no methods in A and B):
public interface C extends A {
void hello();
}
The new abstract hello method in C takes priority over the default hello method from interface A
because C is more specific. Therefore, class D now needs to provide an explicit implementation
for hello; otherwise the program won't compile.
C++ diamond problem
The diamond problem is more complicated in C++. First, C++ allows multiple inheritance of
classes. By default, if a class D inherits from classes B and C, and classes B and C both inherit
from A, then class D will actually have access to a copy of a B object and a copy of a C object. As
a result, uses of methods from A have to be explicitly qualified: are they coming from B or are
they coming from C? In addition, classes have state, so modifying member variables from B isn't
reflected on the copy of the C object.
Search WWH ::




Custom Search