What does <T extends mean?
Bounded Type Parameters:
There may be times when you'll want to restrict the kinds of types that are allowed to be passed to a type parameter. 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. Example:
Following example illustrate how extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces). This example is Generic method to return the largest of three Comparable objects:
public class MaximumTest
{
// determines the largest of three Comparable objects
public static <T extends Comparable<T>> T maximum(T x, T y, T z)
{
T max = x; // assume x is initially the largest
if ( y.compareTo( max ) > 0 ){
max = y; // y is the largest so far
}
if ( z.compareTo( max ) > 0 ){
max = z; // z is the largest now
}
return max; // returns the largest object
}
public static void main( String args[] )
{
System.out.printf( "Max of %d, %d and %d is %d\n\n",
3, 4, 5, maximum( 3, 4, 5 ) );
System.out.printf( "Maxm of %.1f,%.1f and %.1f is %.1f\n\n",
6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) );
System.out.printf( "Max of %s, %s and %s is %s\n","pear",
"apple", "orange", maximum( "pear", "apple", "orange" ) );
}
}
It is called a generic method. This whole concept is called "Generics" in Java. That declaration means T can be any type that is subclass of ABC.
protected <T extends ABC> T save( T Acd, boolean en) {
// ...
}
In this function, there are two places we should pay attention to
- bounded type parameter:
<T extends ABC>
- returned-type:
T
Based on those, I can answer your questions as following
What does it do?
save()
is a generic method that returns a value of type T
. T
is a generic type, that is restricted to ABC
. The scope of T
is limited to save()
.
What is these type of method declarations called in Java?
IMO, the answer should be bounded type parameters, instead of generics. More about generics in Java, you can find here.
One more question I would like to add by myself: Why do we want such thing?
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 [1].