Java bounded type variables

Bounded Type Parameters

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 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).

public class Box  < private T t; public void set(T t) < this.t = t; >public T get() < return t; >public extends Number> void inspect(U u) < System.out.println("T: " + t.getClass().getName()); System.out.println("U: " + u.getClass().getName()); >public static void main(String[] args) < BoxintegerBox = new Box(); integerBox.set(new Integer(10)); integerBox.inspect("some text"); // error: this is still String! > >

By modifying our generic method to include this bounded type parameter, compilation will now fail, since our invocation of inspect still includes a String :

Box.java:21: inspect(U) in Box cannot be applied to (java.lang.String) integerBox.inspect("10"); ^ 1 error

In addition to limiting the types you can use to instantiate a generic type, bounded type parameters allow you to invoke methods defined in the bounds:

public class NaturalNumber  < private T n; public NaturalNumber(T n) < this.n = n; >public boolean isEven() < return n.intValue() % 2 == 0; > // . >

The isEven method invokes the intValue method defined in the Integer class through n.

Multiple Bounds

The preceding example illustrates the use of a type parameter with a single bound, but a type parameter can have multiple bounds:

A type variable with multiple bounds is a subtype of all the types listed in the bound. If one of the bounds is a class, it must be specified first. For example:

Class A < /* . */ >interface B < /* . */ >interface C < /* . */ >class D  < /* . */ >

If bound A is not specified first, you get a compile-time error:

class D  < /* . */ >// compile-time error

Источник

Читайте также:  Построение графиков питон программа
Оцените статью