Using a generic class to perform basic arithmetic operations

No, you can't do that because the + operator is not part of the Number class. What you can do is to create an abstract base class and extends from it:

static void test() {

    MyInteger my = new MyInteger();
    Integer i = 1, j = 2, k;
    k = my.add(i, j);
    System.out.println(k);
}

static public abstract class GenericClass<E extends Number> {
    public abstract E add(E x, E y);
}

static public class MyInteger extends GenericClass<Integer> {
    @Override
    public Integer add(Integer x, Integer y) {
        return x + y;
    }       
}

(I made these classes static in order to facilitate the testing but you can remove this modifier.)

You could also add an abstract function that will take and return parameters and return value of type Number and override it and the subclasses but the required casting for the return value will defeat its usefulness.


No, there isn't a way to do this, or else it would be built into Java. The type system isn't strong enough to express this sort of thing.

Tags:

Java

Generics