Say we want to define a class that allows basic arithmatic, called 'Addable'. Addable things can be added.
abstract class Addable
{
public abstract Addable Add(Addable X, Addable Y)
}
What is the correct way to implement an Addable? The following doesn't work, it gives:
Number does not implement inherited abstract member Addable.Add(Addable, Addable).
class Number : Addable
{
public int Value;
public Number(int Val)
{
Value = Val;
}
public Number Add(Number X, Number Y)
{
return new Number(X.Value + Y.Value);
}
}
I think the problem is that Add takes (Number,Number) as its arguements which aren't generic enough, but I do not know how to proceed.
Edit: As some people have requested to know what this is to be used for, let me elaborate. I am using an algorithm that relies on taking the maximum of several objects. Depending on the use case, these objects are numbers, or distributions. To stay with the example above I will pretend I need to add these numbers or distributions. So I want to have code that looks something like:
Addable LongAlgorithm(Addable X, Other parameters)
{
... // Lots of code that may contain X
Z = Add(X,Y)
... // Code Using Z.
return Answer // Answer is of the same type as X in the input.
}
Edit 2: With the feedback given this question seems to be drifting into the realm of "Interface vs Base class". Perhaps others who read this question might find that question illuminating.
I hope the question is clear, I am new to S.O. and although I have tried to stick to the guidelines as much as possible, I will be happy to modify the question to make it clearer.