I am facing difficulty in writing a generic class in java whose generic members can be compared.
Following is the code snippet I have written, but it is giving me compilation error.
public class TestClass <E extends Comparable<E>>{
private E data1;
private E data2;
public void fun(){
if(data1 > data2){
}
}
}
It is giveing me error by saying that compare(‘>’) operations is not valid.
As per my understanding if I am extending Comparable I should be able to this operation.
Java isn’t C++, and doesn’t have operator overloading. You need to use
.compareTo(), which is provided by theComparableinterface. For example: