Home core java Bounded type parameters in generics

Bounded type parameters in generics

Unbounded types vs Bounded Types :

Unbounded Types :

If there is no restriction to the types that can be used as type arguments in a parameterized type then such types are called Unbounded Types.
As the type parameter we can pass any type.

Example :

class UnboundedTest<T>{
    /* ... */    
}
 
  UnboundedTest<String> u1 = new UnboundedTest<String>();
  UnboundedTest<Long> u2 = new UnboundedTest<Long>();
  //
class UnboundedTest<T>{    /* … */    }   UnboundedTest<String> u1 = new UnboundedTest<String>();  UnboundedTest<Long> u2 = new UnboundedTest<Long>();  //

Bounded Types :

As the type parameter we can not pass any type, there are restrictions, we can bound type parameter to a particular parameter and range by using extends keyword, such types are called Bounded Types.

Syntax :

 class BoundedTest<T extends X>{
       /* ... */
  }
 //
  1. X can be a class or interface.
  2. If X is a class then as a type parameter we can use either X type or its child classes.
  3. If X is interface then as a type parameter we can use either X type or its implementation class Type.
// Example1: 
// We know that all wrapper classes(Integer,Long,Double,Boolean .. etc) are Sub Classes of Number class
 
class BoundedTest<T extends Number>{
    
  }
 
 BoundedTest<Integer> u1 = new BoundedTest<Integer>();//Correct
 BoundedTest<Double> u2 = new BoundedTest<Double>();//Correct
 BoundedTest<String> u3 = new BoundedTest<String>();//Compile Time Error(Type parameter String is not in its bound)
//Example2: We know that Thread class is implementation class of Runnable interface
 
 class BoundedTest<T extends Runnable>{
    
 }
 
 BoundedTest<Runnable> u1 = new BoundedTest<Runnable>();//Correct
 BoundedTest<Thread> u2 = new BoundedTest<Thread>();//Correct
 BoundedTest<String> u3 = new BoundedTest<String>();//Compile Time Error(Type parameter String is not in its bound)

Multiple Bounds :

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

  1. 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.
    If bound A is not specified first, you get a compile-time error.
  2. we can take any number of interfaces.
  3. Only one class is allow to use as type parameter and that class must be first and then followed by interface(s).

For example:

Class A { /* ... */ }
interface B { /* ... */ }
interface C { /* ... */ }
interface D { /* ... */ }
 
class BoundedTest<T extends B & C & D> { /* ... */ } // Correct
class BoundedTest<T extends A & B & C> { /* ... */ } // Correct
class BoundedTest<T extends B & A & C> { /* ... */ }  // compile-time error
//

Note : In the bounded types we are not allowed to use implements and super keywords.
Bounded Types Demo Example :

public class BoundedDemo<T extends Number> {
    
    T t;
    
    public BoundedDemo(T t) {
        this.t = t;
    }
    
    void display(){
            System.out.println("The Type of Value is : "+t.getClass().getName());
        System.out.println("Value is : "+t);
    }
 
    public static void main(String[] args) {
        BoundedDemo<Integer> b1 = new BoundedDemo<Integer>(10);
        b1.display();//Output: The Type of Value is : java.lang.Integer , Value is : 10
        
        BoundedDemo<Double> b2 = new BoundedDemo<Double>(10.5);
        b2.display();//Output: The Type of Value is : java.lang.Double, Value is : 10.5
        
        BoundedDemo<String> b3 = new BoundedDemo<String>("10");//Compile time error - Bound mismatch: 
    }
}

In this article Covered Answers :
1. What is Bounded Types in java generics?
2. What are the type parameter we can use as bounded types in java generics?
3. What is Unbounded Type in java generics?
4. Example to demonstrate Bounded types in java generics
5. Can I use implements or super as bounded type parameter in java?
6. Multiple bounded types
7. Can I use multiple classes as bounded type parameters in java?
8. Can I use multiple interfaces as bounded type parameters in java?

Prashanth Kumar Chilukamari
Prashanth Kumar Chilukamari
Prashanth is a post graduate in master of computer applications, and a professional software engineer with focus on Java, Spring boot, micro services and pivotal cloud. Follow him on LinkedIn or Twitter or Facebook.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories