I was trying to learn Generics in Java.
I created a box class.
package com.generic;
public class Box<T> {
T length;
T breadth;
// Setter and getter
}
Now I wanted to create a method to caluclate area of the box which will be length * breadth
.
I have made it generic so I can use Long , Double, Integer etc.
But when i tried to create a method calculateArea like this
T area(T w , T h)
{
return (w * h);
}
But it is giving me compile time error.
I think multiplication can't be performed on generics.
So what i can do to make my area
method generic?
Thanks.