In Java generics, What is the advantage of using class GenericStack<T extends Object> {}
over class GenericStack<T>{}
.
I have implemented a generic stack using both of the above approaches but unable to trace out the difference. Help me to understand this.
extends Number> represents a list of Number or its sub-types such as Integer and Double. Lower Bounded Wildcards: List<? super Integer> represents a list of Integer or its super-types Number and Object.
List<T> is a name of a generic class. List<Object> is its concrete instantiation. List<T> is not a class yet (it's a generic class, a template you can create concrete classes from but not a class you can use right away), List<Object> is a class.
super T denotes an unknown type that is a supertype of T (or T itself; remember that the supertype relation is reflexive). It is the dual of the bounded wildcards we've been using, where we use ? extends T to denote an unknown type that is a subtype of T .
super is a lower bound, and extends is an upper bound.
There's no difference. <T>
and <T extends Object>
are equivalent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With