This arises often to me: I have a class A which is extended by class B and then there are classes which derive from either class A or both (multiple inheritance).
Example: I have an object representing a mathematical model which defines an interface IModel to apply it and find its coeffefficients.
Based on that, I derive PolynomialModel which defines the large class of polynomial models.
On the other hand, I define a third interface "IAdaptiveModel" which defines operators such as "+" and "-" to adaptively create models. For a polynomial model, this would add their coefficients. On the other hand, for a neural network this would not make sense.
Should IAdaptiveModel now derive from IModel (thus giving rise to the diamond problem) or should this be avoided?
Advantage: When I do a runtime check for IAdaptiveModel I can be sure that it is also an IModel which is supposed to be. Disadvantage: E.g. PolynomialModel would inherit IModel twice.
The question does not target a specific programming language but here is a pseudo code:
classdef IModel
methods (Abstract)
y = apply(obj, x);
obj = solve(obj, x, y);
end
end
classdef IAdaptiveModel % should this derive from IModel ??
methods (Abstract)
obj = plus(obj, other);
obj = minus(obj, other);
end
end
% derives from IModel and can be used by objects requiring apply/solve
% derives from IAdaptiveModel to be used from adaptive algorithms which use plus/minus
classdef PolyModel < IBehavioralModel & IAdaptiveModel
[...]
end
% in a different piece of code in an adaptive algorithm I would have
if isa(modelObject,'IAdaptiveModel')
% technically this could fail because solve does not need to be implemented when IAdaptiveModel does not derive from IModel
modelObject = modelObject.solve(x,y);
modelObject = modelObject + const * deltaModel;
else
error('We require an object that supports IAdaptiveModel');
end