You'll have to use a bounded type parameter:
public <T extends Number> double add (T one, T two)
{
return one.doubleValue() + two.doubleValue();
}
Note that it uses double as return type because that's the primitive numeric type that covers the largest range of values - and one or both parameters could be double
too. Note that Number
also has BigDecimal
and BigInteger
as subclasses, which can represent values outside the range of double
. If you want to handle those cases correctly, it would make the method a lot more complex (you'd have to start handling different types differenty).