I have the following interface:
public interface ISort<T extends Comparable<T>> {
public void sort(T[] array);
}
My understanding of this is that the the <T extends Comparable<T>>
tells the compiler that within this class, there may be some generic types T
, and they must be a Comparable<T>
or be any class which implements Comparable<T>
.
I then have the following class:
public class Sort<T extends Comparable<T>> implements ISort<T> {
public void swap(T[] array, int i, int j) {
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public void sort(T[] array) {
java.util.Arrays.sort(array);
}
}
Again, I have the <T extends Comparable<T>>
telling the compiler that within this class I will be using the type T
which must form an IS-A relationship with Comparable<T>
. However, why must I only type implements ISort<T>
, why do I not need to write implements ISort<T extends Comparable<T>>
? In order to help me understand this could you explain what exactly these generics statements are inferring to the compiler?