Considering the rules of good OOP and design, is it correct to write a method in an interface which has as input types or output types classes which implements the interface?
I give you an example of a very simple exercise I must do, regarding complex numbers:
public interface IComplex
{
double GetImaginary();
double GetReal();
ComplexImpl Conjugate();
ComplexImpl Add(ComplexImpl y);
}
/*----------------------------------------------------*/
public class ComplexImpl : IComplex
{
private double real;
private double imaginary;
public ComplexImpl(double real, double imaginary) { this.real = real; this.imaginary = imaginary; }
public double GetReal() { return this.real; }
public double GetImaginary() { return this.imaginary; }
public ComplexImpl Conjugate()
{
ComplexImpl conjugate = new ComplexImpl(this.real, -this.imaginary);
return conjugate;
}
public ComplexImpl Add(ComplexImpl y)
{
ComplexImpl result = new ComplexImpl(this.real + y.real, this.imaginary + y.imaginary);
return result;
}
}
Considering the interface IComplex: is it correct to write the methods Conjugate and Add such that they have as input (and/or output) objects which are instantiations of the class ComplexImpl?
Consider that the class ComplexImpl implements the interface when this methods are defined.
EDIT:
For those of you who answer: first of all thank you, however I have the following problem. If I substitute "ComplexImpl" with "IComplex" both in the interface and in the class, I obtain this error in the method Add:
"'IComplex' does not contain a definition for 'imaginary' and no accessible extension method 'imaginary' accepting a first argument of type 'IComplex' could be found".
The only way to solve this is through the use of Generics?