I have the following hierarchy of type-ized interfaces and one implementation:
public interface Operation<T> {
T add(T object1, T object2);
}
public interface OperationNumeric<T extends Number> extends Operation<T> {
T add(T number1, T number2);
}
And this implementation with an overloaded method add():
public class OperationFloat implements OperationNumeric<Float> {
public Float add(Float number1, Float number2) {
return number1+number2;
}
public Float add(Float number1, Integer number2) {
return number1+number2;
}
}
Now I have a method that uses OperationNumericInterface:
public Number calculate(Number operand1, Number operand2, OperationNumeric operand) throws Exception {
return operand.add(operand1, operand2);
}
Now I'm trying to call calculate() intentionally with two different types Float and Integer like this:
try {
calculate(2.2f,2, new OperationFloat());
} catch (Exception e) {
System.out.println(e.getMessage());
}
Once I call it, Java keeps referring to add(Float, Float) method and cannot see the overloaded method add(Float, Integer).
IDEA suggests "Unchecked call to add(T, T) as a member of raw type ..."
How do I modify my interfaces/classes to be able to use overloaded method as I want?