If I were to create a class and that class is intended to only accept a specific type of data. I would proceed to use generics and in this class I will proceed to have the type constrained to the upper bound of the Number class.
So this class can only use types of the Number class or the subclasses of Number (Integer, Double, Long, etc).
Lets now say I have an array of these types and I wish to compare values in that array. So this class contains an array of only these allowed data types, and we wish to compare these types only.
We can do this by using the comparable interface which contains the compareTo method. You can only use the compareTo method with classes that implement the Comparable interface. Number is one of the classes that implement this interface.
So If I have a class header that specifies the following:
public class SomeName<T extends Number> {
This successfully implies the constraint of the type T to only accept types of Number or subclasses of number. If I wanted to use the compareTo method from the Comparable interface, which Numbers class implements, which T is constrained to.
If I were to have a method such as
public T someName(T[] SomeVariable) {
Why can't that method use the compareTo method?T is constrainted to the Number class, which implements Comparable.
If I were to have
public <T extends Comparable<T>> someName(T[] SomeVariable) {
Then it works because T extends comparable, which is the samething as T implements comparable in generic notation... Isn't that practically the same thing as above though?