HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

5) Generics Lesson

Generics with Bounded Types

5 min to complete · By Ryan Desmond

When declaring a generic type in Java, you can also limit the scope of types that are allowed in the created object by creating a bounded type parameter.

What is a Bounded Type Parameter

A bounded type parameter allows a generic object to limit the type chosen for all of its content and instances.

For example, imagine you have a generic class that handles various mathematical functions. You do not want people to be able to create an instance of that class and pass in a String as the type parameter. If they did, all the mathematical functions would fail because you can't do math on strings.

How to Create a Bounded Type Parameter

To declare a bounded type parameter, after including the type parameter's name, you use the keyword extends followed by the class that denotes the upper type bound. Below is the syntax for creating a generic method.

public <T extends B1> void method_name(){}

Multiple Bounded Type Parameter

In the method example above, there is a single bounded type parameter. In Java, you also have the ability to create a multiple bounded type parameter, as seen in the example syntax below:

public <T extends B1 & B2 & B3> void method_name(){}

A type variable possessing multiple bounds is considered a subtype of every type mentioned in the binding. In cases where one of the bounds is a class, it is necessary for that class to be explicitly specified as the first bound. For example:

Class A { /* ... */ }

interface B { /* ... */ }

interface C { /* ... */ }

class D <T extends A & B & C> { /* ... */ }

Single Bounded Type Parameter Example

In the example below, you can see the bounded type being applied to a generic method.

/* this class is Generic - class does not use 
bounded types - see inspect() method below */
public class Box<T> {
  private T t;         

  public void set(T t) {
    this.t = t;
  }

  public T get() {
    return t;
  }     

  /* the method below uses bounded types 
      it is Generic - but only accepts data 
      types that are subclass of "Number" 
      if this was not done, the main() method 
      below would fail */
  public <U extends Number> void inspect(U u){
    System.out.println("T: " + t.getClass().getName());
    System.out.println("U: " + u.getClass().getName());
  }

  /* if a non-number was passed to this 
      method, it would fail due to the 
      multiplication below */
  public <U extends Number> double square(U u){
    return u.doubleValue() * u.doubleValue();
  }
 
  public static void main(String[] args) {
    Box<Integer> integerBox = new Box<Integer>();
    integerBox.set(new Integer(10));
    
    // error - inspect() only accepts 
    // subclasses of "Number"
    integerBox.inspect("some text");    
  }
}

Summary: Java Generics with Limited Type

  • Objects with generic types can limit the possible chosen types
  • A bounded type parameter is applied using the extends or implements keywords
  • Bounded type parameters can contain single or multiple bounds
  • Multiple bounds are separated using the & character

Syntax

Here's the syntax for creating a bounded type parameter, where you can substitute the variables starting with your_ with your values.

<Your_Type_Parameter extends Your_Bound>