Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The syntax <T extends Class<T>> in Java

I have couple of thoughts regarding the following:

public interface MaxStack<T extends Comparable <T>>

1-Why does the class that implements MaxStack should be written like this:

public class MaxStackclass<T extends Comparable <T>> implements MaxStack<T>

and not public class MaxStackclass<T extends Comparable <T>> implements MaxStack<T extends Comparable <T>>?

2- why do the private variables of this class, when I use generics, should be written only with <T> and not with <T extnds Comparable<T>>? For example, private List<T> stack= new ArrayList<T>();

3-What is the difference between <T extends Comparable<T>> and <T extends Comparable>- if I need to compare bewteen elements in my class, both will be O.K, no?

Edit: I think that thee problem with 3 is that maybe it allows to insert of a list that was defined in the second way to have different elements which all extends from comparable and then when I want to compare them, it won't be possible, since we can't compare String to Integer, both extend from Comparable.

like image 402
Numerator Avatar asked Sep 02 '11 11:09

Numerator


People also ask

What does T extends Comparable t mean in Java?

Java- The meaning of <T extends Comparable<T>> ? a) Comparable <T> is a generic interface (remember it's an "interface" i.e not a "class") b) extends means inheritance from a class or an interface.

What is the T class in Java?

< T > is a conventional letter that stands for "Type", and it refers to the concept of Generics in Java. You can use any letter, but you'll see that 'T' is widely preferred. WHAT DOES GENERIC MEAN? Generic is a way to parameterize a class, method, or interface.

What is the meaning of class T?

In Java there's a single metaclass: Class . Its instances (only one per type exists) are used to represent classes and interfaces, therefore the T in Class<T> refers to the type of the class or interface that the current instance of Class represents. Follow this answer to receive notifications.

What is difference between list extends T and list Super T?

extends T represents an upper bounded wildcard. The unknown type represents a type that must be a subtype of T, or type T itself. ? super T represents a lower bounded wildcard.


1 Answers

  1. In the declaration maxStackclass<T extends Comparable <T>> you have already expressed the bounds on T. So you do not need it again.

  2. Reason same as above. No need to specify bounds on the same type parameter again.

  3. <T extends Comparable<T>> means that T must implement the Comparable interface that can compare two T instances. While <T extends Comparable> means T implements Comparable such that it can compare two Objects in general. The former is more specific.

if I need to compare bewteen elements in my class, both will be O.K, no?

Well, technically you can achieve the same result using both. But for the declaration <T extends Comparable> it will involve unnecessary casts which you can avoid using the type safe <T extends Comparable<T>>

like image 126
Swaranga Sarma Avatar answered Oct 13 '22 00:10

Swaranga Sarma