You are on page 1of 4

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.

For example, If you want a generic class that works only with numbers (like int, double, float,
long …..) then declare type parameter of that class as a bounded type to java.lang.Number class.
Then while creating objects to that class you have to pass only Number types or it’s subclass
types as type parameters.

Here is the syntax for declaring Bounded type parameters.

<T extends SuperClass>


This specifies that ‘T’ can only be replaced by ‘SuperClass’ or it’s sub classes. Remember
that extends clause is an inclusive bound. That means bound includes ‘SuperClass’ also.
Example 1:

class Gen
{
static < S , T extends Number> void display(S t, T v)
{
System.out.println(v.getClass().getName()+" = " +v);
System.out.println(t.getClass().getName()+" = " +t);
}
public static void main(String[] args)
{
display("88",6); // CASE 1: S can be any data but T both should be a Number ( < S , T extends Number>)
//display(1,2); //S T both should be a Number ( < S extends Number, T extends Number>)
//display("100","C"); // S T both should not be a Number ( < S , T>)

}
}

Output:

java.lang.Integer = 6
java.lang.String = 88

Output 2:

Compile Errors :

prog.java:12: error: method display in class Gen cannot be applied to given types;
display("100","C"); // S T both should not be a Number ( < S , T>)
^
required: S,T

Example 2:

public class MaximumTest


{
// determines the largest of three Comparable object
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("Max 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","A", "B", "C", maximum("A", "B", "C"));

System.out.printf("Max of %s, %s and %s is %s\n","Apple", "Ball", "Car",


maximum("Apple", "Ball", "Car")); }
}

OUTPUT:

Max of 3, 4 and 5 is 5

Max of 6.6,8.8 and 7.7 is 8.8

Max of A, B and C is C

Max of Apple, Ball and Car is Car


Generic Restrictions

In this chapter you will learn:

1. Type Parameters Can't Be Instantiated


2. What are restrictions on static members
3. What are restrictions on generic array

Type Parameters Can't Be Instantiated

It is not possible to create an instance of a type parameter. For example,


consider this class:
// Can't create an instance of T.
class Gen<T> {
T ob;/*java 2 s .c om*/

Gen() {
ob = new T(); // Illegal!!!
}
}

Restrictions on Static Members

No static member can use a type parameter declared by the enclosing class.
For example, all of the static members of this class are illegal:
class Wrong<T> {
// Wrong, no static variables of type T.
static T ob;
// ja v a2 s . c o m
// Wrong, no static method can use T.
static T getob() {
return ob;
}

// Wrong, no static method can access object of type T.


static void showob() {
System.out.println(ob);
}
}

You can declare static generic methods with their own type parameters.
Generic Array Restrictions

You cannot instantiate an array whose base type is a type parameter. You
cannot create an array of type specific generic references.

The following short program shows both situations:


class MyClass<T extends Number> {
T ob;/* ja va 2 s . co m*/
T vals[];

MyClass(T o, T[] nums) {


ob = o;
vals = nums;
}
}

public class Main {


public static void main(String args[]) {
Integer n[] = { 1 };
MyClass<Integer> iOb = new MyClass<Integer>(50, n);
// Can't create an array of type-specific generic references.
// Gen<Integer> gens[] = new Gen<Integer>[10];
MyClass<?> gens[] = new MyClass<?>[10]; // OK
}
}

You might also like