I'm new to Java & have learned Generics Today. Im facing this problem & I don't know what I did wrong here!! I really appreciate any help you can provide:)
//Practice Que:
//AIM of this code:: to pass any "Number" type(byte, int, float...) and get its sum,sub,mul
class Operations<T extends Number> {
T x1, x2;
public Operations(T x1, T x2) {
super();
this.x1 = x1;
this.x2 = x2;
}
public void Arithmetic() {
// ----- **WHY ERROR???** => The operator +,-,* is undefined for the argument type(s) T, T ------------
System.out.println("sum: " + (x1 + x2));
System.out.println("sub: " + (x1 - x2));
System.out.println("mul: " + x1 * x2);
// ----------------------------------------------------------------------------------------
}
}
public class Example1 {
public static void main(String[] args) {
// for Integer
Operations<Integer> o1 = new Operations<>(2, 3);
o1.Arithmetic();
// for Float
Operations<Float> o2 = new Operations<>(2.5f, 3.5f);
o2.Arithmetic();
}
}