Livia Mitrică
Livia Mitrică

Reputation: 43

Java Interfaces type declarations for methods

I have an interface which contains some arithmetic operations. I have created a class for complex numbers and it will implement the operations.

package numeric;
public interface Numeric {

    public void add();
    public void subtract();
    public void multiply();
}

My class looks something like this

package numeric;
public class Complex implements Numeric {

    private int Re, Im;
    public void add (Complex x){
        this.Re+=x.Re;
        this.Im+=x.Im;  
    }
}

My methods currently have no parameters in the interface as I do not know which type to make them because I have to use the same interface for fractions also. If I put Complex when I implement them in the class it will give an error saying that the class must implement all interface methods.

Upvotes: 2

Views: 69

Answers (1)

ernest_k
ernest_k

Reputation: 45309

An approach you can use is to force each Numeric sub-type convert to each other (a little like the intValue, longValue, doubleValue, etc. methods defined in Number, although the purpose is slightly different).

interface Numeric {
    public void add(Numeric other);

    public void subtract(Numeric other);

    public void multiply(Numeric other);

    public Complex toComplex();

    public Fraction toFraction();
}

This has cons and pros, one of the cons being that your interface is now bound to know all of its implementations (not sure what this is called, sealed types?)

The above can then be implemented as follows:

class Complex implements Numeric {

    private int Re, Im;

    @Override
    public void add(Numeric other) {
        Complex complex = other.toComplex();
        this.Re += complex.Re;
        this.Im += complex.Im;
    }

    @Override
    public void subtract(Numeric other) {
        Complex complex = other.toComplex();
        this.Re -= complex.Re;
        this.Im -= complex.Im;
    }

    @Override
    public Complex toComplex() {
        return this;
    }

    @Override
    public Fraction toFraction() {
        //convert to fraction
        return null;
    }
    //rest of the implementation
}

Just a side note: typically, such APIs encourage immutability of data structures, so it would be advisable to change it to something like:

interface Numeric {
    public Numeric add(Numeric other);

    public Numeric subtract(Numeric other);

    public Numeric multiply(Numeric other);

    public Complex toComplex();

    public Fraction toFraction();
}

And

class Complex implements Numeric {

    private final int re, im;

    public Complex(int re, int im) {
        this.re = re;
        this.im = im;
    }

    @Override
    public Numeric add(Numeric other) {
        Complex complex = other.toComplex();
        return new Complex(this.re + complex.re, this.im + complex.im);
    }

    @Override
    public Numeric subtract(Numeric other) {
        Complex complex = other.toComplex();
        return new Complex(this.re - complex.re, this.im - complex.im);
    }

    @Override
    public Complex toComplex() {
        return this;
    }

    @Override
    public Fraction toFraction() {
        //convert to fraction
        return null;
    }

    //rest of the implementation
}

Upvotes: 1

Related Questions