Bounded Type Parameters

This section provides a tutorial example on creating a generic class with a bounded type parameter so that only certain types are allowed for the type parameter.

What Is a Bounded Type Parameter? A bounded type parameter is type parameter that can be parameterized only with certain limited types. This limitation on type parameters allow you to create generic classes to offer special functions to those limited types.

The syntax to declare a bounded type parameters is to add the keyword "extends" with one or more base types. Examples are listed below:

class Name<T extends C> {}
class Name<T extends I> {}
class Name<T extends I1 & I2...> {}
class Name<T extends C & I1 & I2...> {}

Here is a tutorial example that shows you how to create a generic class with a bounded type parameter:

/* AddRegister.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
class AddRegister<T extends Number> {
   private double sum;

   public AddRegister() {
      sum = 0.0;
   }

   public void add(T item) {
      sum += item.doubleValue();
   }
   
   public double getSum() {
      return sum;
   }

   public static void main(String[] a) {
      java.io.PrintStream o = System.out;
      AddRegister<Float> reg = new AddRegister<Float>();
      reg.add(new Float(99.99));
      reg.add(new Float( 6.50));
      reg.add(new Float(11.99));
      o.println("Total price: "+reg.getSum());

      // not allowed - Wrong type is given the type parameter
      // AddRegister<String> test = new AddRegister<String>();
   }
}

The example runs ok. See the output below:

Total price: 118.4799976348877

If you uncomment the not allowed line in the example, you will get a compilation error:

AddRegister.java:28: error: type argument String is not within bounds 
   of type-variable T
      AddRegister<String> test = new AddRegister<String>();
                  ^
  where T is a type-variable:
    T extends Number declared in class AddRegister
AddRegister.java:28: error: type argument String is not within bounds 
   of type-variable T
      AddRegister<String> test = new AddRegister<String>();
                                                 ^
  where T is a type-variable:
    T extends Number declared in class AddRegister
2 errors

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

Generic Classes and Parameterized Types

 What Is a Generic Class?

 Using a Generic Class

 Using a Generic Class - Example

 Creating a Generic Class

 Creating a Generic Class - Example

Bounded Type Parameters

 Raw Type, Generic Type and Parameterized Type

 Parameterized Type and Subtyping

 Wildcard Parameterized Types

 Wildcard Parameterized Type Test

 Wildcard Parameterized Subtyping

 Wildcard Parameterized Subtyping Example

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version