Reputation: 39437
Reading this here...
https://docs.oracle.com/javase/tutorial/java/generics/bounded.html
"To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound, which in this example is Number. Note that, in this context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces)."
... but after doing a few tests it seems the upper bound is included. So I think the part in bold is not quite precise. The general sense here is "extends or same type" or "implements or same type".
Is my understanding correct?
To give a student-like example... Say I have this method
public static <U extends Animal> void inspect(U u){
System.out.println("U: " + u.getClass().getName());
}
Then it seems I can well pass in an Animal
object to it i.e. the argument doesn't need to be a Dog
, or Cat
or any actual subclass of Animal, it can well be just an Animal
(I am assuming that Animal
is not abstract of course).
Upvotes: 0
Views: 604
Reputation: 159076
The full quote is (emphasis mine):
There may be times when you want to restrict the types that can be used as type arguments in a parameterized type. For example, a method that operates on numbers might only want to accept instances of
Number
or its subclasses. This is what bounded type parameters are for.To declare a bounded type parameter, list the type parameter's name, followed by the
extends
keyword, followed by its upper bound, which in this example isNumber
. Note that, in this context,extends
is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces).
To summarize: When the upper bound is Number
, the type parameter accept instances of Number
or its subclasses.
In short: The upper bound is inclusive.
Upvotes: 2