Articles

Bounded Type Parameter in Java Generics

Share

In Java Generics, a situation can arise where we need to restrict or bound an object type to specific types only in order to narrow down the acceptable types supported in the type parameter. For instance, a method that deals with numbers, should accept Number and its sub-classes.

Bounded Types:

The concept of limiting type parameter upto certain type is known as bounded types. In this, extends keyword is used to bound the type parameter.

It simply tells the compiler to allow only parent class or its subclasses types that T has extended, meaning,it restricts in passing types.

For example, if T extends Number, then only the type of Number class itself or its child classes such as integer,double and others are applicable.

Point to remember:

– In generics, one can extends an interface too.

– Super keyword terminology is not applied with Type Parameter

– extends keywords is used for bounded types

Syntax :

class A <T extends AnyClassName or InterfaceName> {​ // it can be class or interface

​
}​

Java program to demonstrate Bounded Type Parameter

public class BoundedTypeExample<T extends Number> {

void print(T num){
System.out.println("Only Number is allowed "+ num);
}

public static void main(String[] args) {
BoundedTypeExample<Integer> boundedTypeExample = new BoundedTypeExample();
boundedTypeExample.print(4);

}
}

However, passing string type would trigger compile time error.

public class BoundedTypeExample<T extends Number> {

void print(T num){
System.out.println("Only Number is allowed "+ num);
}

public static void main(String[] args) {
BoundedTypeExample<String> boundedTypeExample = new BoundedTypeExample(); // Type parameter 'java.lang.String' is not within its bound; should extend 'java.lang.Number'
boundedTypeExample.print("4");

}
}

Is it Possible to extend class and interface together in generics ?

When there is a need of having type that must be type bounded to a class and at the same time, it  should implement an interface. For such situation, there is a way to achieve this functionality.

Syntax:

class ClassName < T extends AnyClassName & AnyInterfaceName >{

}

The syntax simply means, an object of type T must extend the given class and implement the interface at the same time. This is another way to define bounded type.

It is important to remember that when using class and interface in combination, always class comes first and then after interfaces.
However, multiple classes cannot be extended since java doesn’t allow Multiple inheritance concept.

Java Program to demonstrate Bounded Type Parameter with combination of Class and Interface

public class BoundedTypeExample<T extends Number & Serializable> {

void print(T num){
System.out.println(num);
}

public static void main(String[] args) {
BoundedTypeExample<Integer> boundedTypeExample = new BoundedTypeExample<>();
boundedTypeExample.print(2);

}
}

Explanation: Above example, simply prints integer value, and integer is subclass of Number and also Implement Serializable interface.

Sandeep Verma

Published by
Sandeep Verma
Tags: Bounded type Parameter bounded types Generic Class generic concept in Java Generic Programming in Java generics java