0

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
divB
  • 896
  • 1
  • 11
  • 28
  • Can you show some code and also tell what language you use? – codekiddy Jul 31 '14 at 06:18
  • It does not target a certain language - it's a conceptual OOP question. I still added code from which it should be clear. – divB Jul 31 '14 at 06:54
  • I believe your `IAdaptiveModal` and `IModal` are interfaces. I don't see how you can encounter with the diamond problem if you use interfaces. – sampathsris Jul 31 '14 at 06:58
  • Yes, in this case they are interfaces. But I also encounter cases where they are not. Then a different example: A signal class defines and implements generic functions for a sampled signal. From this I derive "BandlimitedSignal". I also derive "RandomSignal" from it and from RandomSignal "ComplexRandomSignal". A class "BandlimitedComplexRandomSignal" would derive from BandlimitedSignal and ComplexRandomSignal and therefore implicitely twice from the first Signal class. The question is more if this is bad coding style in general and should be avoided. – divB Jul 31 '14 at 07:03

0 Answers0