1

How do I implement void add(Number number) so it adds number to the object instance

public interface Numbers {
   static int toIntValue();
   static void fromIntValue(int value);
   default void add(Number number) {
        // what do i write here
    }
}
nsol
  • 75
  • 7
  • Interfaces don't know about object states. Typically, `add` would be an abstract method too. Why do you want to have it implemented in the interface? – ernest_k Dec 08 '19 at 15:49
  • 1
    What is the purpose of `fromIntValue`? Normally a method called `from` would have a return value. – khelwood Dec 08 '19 at 15:58
  • it returns the value of the object instance as int – nsol Dec 08 '19 at 16:00
  • 1
    But as @khelwood said, it doesn't. The return type is `void`. Moreover, such a method would normally be `static`. – T.J. Crowder Dec 08 '19 at 16:00
  • fromIntValue it assigns the object instance the parameter value – nsol Dec 08 '19 at 16:02
  • @nsol - It can't, if it's `static`. There's no instance to assign to. Before you added `static`, yes, it could have assigned to some field on the instance (**not** to the instance itself). But that would be odd for a method with that name. Conventions are a useful part of programming. – T.J. Crowder Dec 08 '19 at 16:15
  • Now you've added the `static` keyword, but that doesn't make sense. Static methods cannot be abstract. – khelwood Dec 08 '19 at 16:39

1 Answers1

1

You mostly cannot do this; interfaces do not have any state, and the notion of 'add a number' strongly implies that you wish to update the state.

This is one way to go:

public interface Number /* Isn't Numbers a really weird name? */ {
    int toIntValue();
    default int add(int otherValue) {
        return toIntValue() + otherValue;
    }
}

Here no state is changed; instead a new int is returned.

Another problem here is that the whole notion of abstracting away a numeric type is that there is no default implementation of add.

That's just basic math. Complex numbers are a kind of number; it is clearly impossible to write code that can add 2 complex numbers together without knowing anything about complex numbers beforehand.

What you CAN do is create add out of other primitives, except, 'add' is generally the convenient primitive. For example, here's a take on multiply that can work as a default method, though it is not at all efficient:

public interface Number {
    Number plus(Number a); /* an immutable structure makes more sense, in which case 'plus' is a better word than 'add' */
    default Number multiply(int amt) {
        if (amt == 0) return Number.ZERO; // Define this someplace.
        Number a = this;
        for (int i = 1; i < amt; i++) a = a.plus(this);
        return a;
    }
}

Here you've defined multiply in terms of plus.

Note that java already has an abstract number concept (java.lang.Number) and it indeed can do almost nothing, because trying to abstract math like this is hard in any language, and particularly so in java.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Ah, you see, I was mislead by the class name into thinking `Numbers` was a container for `Number` instances. But the other method names really don't support my interpretation. – T.J. Crowder Dec 08 '19 at 15:56