2

I have this interface:

public interface Numeric {
    public Numeric addition(Numeric x,Numeric y);
    public Numeric subtraction(Numeric x,Numeric y);
}

And this class:

public class Complex implements Numeric {
    private int real;
    private int img;

    public Complex(int real, int img) {
        this.real = real;
        this.img = img;
    }

    public Numeric addition(Numeric x, Numeric y) {
        if (x instanceof Complex && y instanceof Complex) {
            Complex n1 = (Complex)x;
            Complex n2 = (Complex)y;

            return new Complex(n1.getReal() + n1.getReal(), n2.getImg() + 
            n2.getImg());                 
        } 
        throw new UnsupportedOperationException();
    }

    public Numeric subtraction(Numeric x, Numeric y) {
        if (x instanceof Complex && y instanceof Complex) {
            Complex n1 = (Complex)x;
            Complex n2 = (Complex)y;

            return new Complex(n1.getReal() - n1.getReal(), n2.getImg() - 
            n2.getImg());                 
        } 
        throw new UnsupportedOperationException();
    }

    public int getReal() {
        return real;
    }

    public int getImg() {
        return img;
    }
}

Why do I get this error:

incompatible types: Numeric cannot be converted to Complex

when I run this code:

public class TestNumeric {
    public static void main(String[] args) {
        Complex c1 = new Complex(3, 4);
        Complex c2 = new Complex(1, 2);
        Complex rez;

        rez = rez.addition(c1, c2);
    }
}

The error is at the line "rez = rez.addition(c1, c2);"
Complex implements Numeric, so every Numeric is a Complex, right? I have already done casting and checking inside the addition method. Why do I get this error, and how can I fix it?

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
  • 1
    `Complex implements Numeric, so every Numeric is a Complex` - no, every `Complex` is a `Numeric`. The opposite is not true. – Eran Dec 09 '19 at 13:57
  • No that's not right. It's the other way round: Every `Complex` is a `Numeric`. – stevecross Dec 09 '19 at 13:57
  • Myself, I'd have addition take **one** parameter only, not two – Hovercraft Full Of Eels Dec 09 '19 at 14:00
  • The addition method can't be static, though, because it's an interface method. – RealSkeptic Dec 09 '19 at 14:01
  • 1
    Your code is not ok because `rez` is not initialized. If you set `rez` of type `Numeric` (and fake-ly initialize the variable the variable to avoid `NPE`) your code will compile. This is fine: `Numeric rez = c1.addition(c1, c2);` but as other says probably would be better changing your method and make like this: `Numeric rez = c1.addition(c2);` – Ermal Dec 09 '19 at 14:03
  • The answer to your previous question already explains this _Every Numeric object is not Complex._ Since you've made your `addition` method have a return type of `Numeric`, you can't assign it to a variable of type `Complex`. – Sotirios Delimanolis Dec 09 '19 at 14:29
  • Assuming it only makes sense to add numerics of the same type, this ought to be generic: `interface Numeric { T addition(T x, T y); T subtraction(T x, T y); }` and then `class Complex implements Numeric { ... }`. – kaya3 Feb 04 '20 at 15:34

1 Answers1

0

The declaration of addition and subtraction should be as follows:

public interface Numeric {
    public Numeric addition(Numeric obj);

    public Numeric subtraction(Numeric obj);
}

and the implementation of addition and subtraction should be as follows:

public Numeric addition(Numeric obj){
    if (obj instanceof Complex){
        Complex n = (Complex)obj;

        return new Complex(this.getReal() + n.getReal(), this.getImg() + 
        n.getImg()); 
    } else {
        throw new UnsupportedOperationException();
    }
}

Finally, TestNumeric should be as follows:

public class TestNumeric {
    public static void main(String[] args) {
        Numeric c1 = new Complex(3, 4);
        Numeric c2 = new Complex(1, 2);
        Numeric rez = c1.addition(c2);
    }
}

[Update]

@OrosTom - Based on your comment, I have added below the method that you need to put inside the class, Complex so that you can print the result

@Override
public String toString() {
    return real + " + " + img + "i";
}

Note: please check https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html (section, The toString() Method) for more details.

After this, the output of the following code

public class TestNumeric {
    public static void main(String[] args) {
        Numeric c1 = new Complex(3, 4);
        Numeric c2 = new Complex(1, 2);
        Numeric rez = c1.addition(c2);
        System.out.println(c1);
        System.out.println(c2);
        System.out.println(rez);
    }
}

will be:

3 + 4i
1 + 2i
4 + 6i
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
  • 1
    I get the idea of having only one parameter, is way better. But i still get that error. –  Dec 09 '19 at 14:09
  • @ArvindKumarAvinash - Making rez of type Numeric seems to solve the error. But why do I have to do that? It's confusing. And after, how do I print the result, if it's of type Numeric? –  Dec 09 '19 at 14:17
  • Please check the **[update]** section of the answer. – Arvind Kumar Avinash Feb 04 '20 at 15:22