Search code examples
javaabstract-classgeneric-programming

Comparable vs Number, Design choice


I have a class which have a structure like

class A {
    String key;
    Double value;
}

Now the value could be double, long, int, fraction and so on.

How should I handle this in the same class?

Shall I make value a Comparable or Number?

Number as these values are extending Number, Comparable as i want this class [and value] to be comparable.


Solution

  • I suggest you to make the class with a generic type <T extends Number>.

    class A<T extends Number> {
        String key;
        T value;
    }