1

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?

1

1 Answer 1

5

Number does not implement Comparable.

The thing to use is:

SomeName<T extends Number & Comparable<? super T>>

At present, you cannot impose extra restrictions on T in an instance method, so if you write

public <T extends Comparable<T>> T someName(T[] SomeVariable)

that's actually a different T.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm not sure where I got number implemented comparable. I must have gotten mixed up. So you are saying that the T type in the class header is different than the T in the method header? It makes sense. I appreciate the help.
Usually if you refer to T in the method, it's the same T as in the top of the class. E.g. in public T someName(T[] SomeVariable), the T is the same as at the top. If you declare T in <> before the return type, it's a generic method and the T is a different type parameter. If you do this you should really use something else, like U, because then you can refer to both T and U.
Thank you, I appreciate your help. I'm going to continue studying this section until I get it down completely. I've made a few programs so far using generics and it helps understand it much more.
@pudge I'm glad I could help. Generics are really, really complicated (probably too complicated). I learn new things about them all the time and still ask questions on this site about them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.