The World’s Largest Online Community for Developers

'; java - What is the benefit of an upper bounded wildcard? - LavOzs.Com

The recurring explanation I find is that an upper bounded wildcard relaxes the restrictions of types that a type parameter can accept. This concept applies to bounded generics as well, for example:

static <T extends Number> void gMethod (ArrayList <T> list) {}

This method's generic will accept Objects of type Number or any of it's sub classes when specified:

ArrayList <Integer> intList = new ArrayList();
gMethod(intList); // allowed

For further elaboration that a generic bounded to Number will accept type arguments of Number or any of it's sub classes as well:

class Thing <T extends Number> {}
Thing <Number> numThing = new Thing();
Thing <Integer> intThing = new Thing();
Thing <Double> dubThing = new Thing(); // All three instances work

Given this, the only benefit I can see to using an upper bounded wildcard vs a bounded generic is that an upper bounded wildcard type argument can be declared without relying on a type parameter already declared by either a class or method. Is there a more important benefit that I'm missing?

What is reflection and why is it useful?
What is the difference between public, protected, package-private and private in Java?
What is a serialVersionUID and why should I use it?
Java Generics Wildcarding With Multiple Classes
What is PECS (Producer Extends Consumer Super)?
What is a JavaBean exactly?
wildcard * in CSS for classes
What is the different between upper bound wild cards with bound type parameters?
Is there a difference when specifying upper bounds for wildcards explicitly?