Bounded Types with Generics in Java
Last Updated :
15 Mar, 2022
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 Numbers or their subclasses. This is what bounded type parameters are for.
- Sometimes we don’t want the whole class to be parameterized. In that case, we can create a Java generics method. Since the constructor is a special kind of method, we can use generics type in constructors too.
- Suppose we want to restrict the type of objects that can be used in the parameterized type. For example, in a method that compares two objects and we want to make sure that the accepted objects are Comparables.
- The invocation of these methods is similar to the unbounded method except that if we will try to use any class that is not Comparable, it will throw compile time error.
How to Declare a Bounded Type Parameter in Java?
- List the type parameter's name,
- Along with the extends keyword
- And by its upper bound. (which in the below example c is A.)
Syntax
<T extends superClassName>
Note that, in this context, extends is used in a general sense to mean either “extends” (as in classes). Also, This specifies that T can only be replaced by superClassName or subclasses of superClassName. Thus, a superclass defines an inclusive, upper limit.
Let’s take an example of how to implement bounded types (extend superclass) with generics.
Java
// This class only accepts type parameters as any class
// which extends class A or class A itself.
// Passing any other type will cause compiler time error
class Bound<T extends A>
{
private T objRef;
public Bound(T obj){
this.objRef = obj;
}
public void doRunTest(){
this.objRef.displayClass();
}
}
class A
{
public void displayClass()
{
System.out.println("Inside super class A");
}
}
class B extends A
{
public void displayClass()
{
System.out.println("Inside sub class B");
}
}
class C extends A
{
public void displayClass()
{
System.out.println("Inside sub class C");
}
}
public class BoundedClass
{
public static void main(String a[])
{
// Creating object of sub class C and
// passing it to Bound as a type parameter.
Bound<C> bec = new Bound<C>(new C());
bec.doRunTest();
// Creating object of sub class B and
// passing it to Bound as a type parameter.
Bound<B> beb = new Bound<B>(new B());
beb.doRunTest();
// similarly passing super class A
Bound<A> bea = new Bound<A>(new A());
bea.doRunTest();
}
}
OutputInside sub class C
Inside sub class B
Inside super class A
Now, we are restricted to only type A and its subclasses, So it will throw an error for any other type of subclasses.
Java
// This class only accepts type parameters as any class
// which extends class A or class A itself.
// Passing any other type will cause compiler time error
class Bound<T extends A>
{
private T objRef;
public Bound(T obj){
this.objRef = obj;
}
public void doRunTest(){
this.objRef.displayClass();
}
}
class A
{
public void displayClass()
{
System.out.println("Inside super class A");
}
}
class B extends A
{
public void displayClass()
{
System.out.println("Inside sub class B");
}
}
class C extends A
{
public void displayClass()
{
System.out.println("Inside sub class C");
}
}
public class BoundedClass
{
public static void main(String a[])
{
// Creating object of sub class C and
// passing it to Bound as a type parameter.
Bound<C> bec = new Bound<C>(new C());
bec.doRunTest();
// Creating object of sub class B and
// passing it to Bound as a type parameter.
Bound<B> beb = new Bound<B>(new B());
beb.doRunTest();
// similarly passing super class A
Bound<A> bea = new Bound<A>(new A());
bea.doRunTest();
Bound<String> bes = new Bound<String>(new String());
bea.doRunTest();
}
}
Output :
error: type argument String is not within bounds of type-variable T
Multiple Bounds
Bounded type parameters can be used with methods as well as classes and interfaces.
Java Generics supports multiple bounds also, i.e., In this case, A can be an interface or class. If A is class, then B and C should be interfaces. We can’t have more than one class in multiple bounds.
Syntax:
<T extends superClassName & Interface>
Java
class Bound<T extends A & B>
{
private T objRef;
public Bound(T obj){
this.objRef = obj;
}
public void doRunTest(){
this.objRef.displayClass();
}
}
interface B
{
public void displayClass();
}
class A implements B
{
public void displayClass()
{
System.out.println("Inside super class A");
}
}
public class BoundedClass
{
public static void main(String a[])
{
//Creating object of sub class A and
//passing it to Bound as a type parameter.
Bound<A> bea = new Bound<A>(new A());
bea.doRunTest();
}
}
OutputInside super class A
Similar Reads
Generic Set In Java The Set interface is present in java.util package. It is basically a collection of objects with no duplicate objects that means there can be no two objects in a Set e1 and e2 such that e1.equals(e2) and at most one null element. It is an interface that models the mathematical set. This interface inh
3 min read
Type-Safety and Type-Casting in Java Generics Generics means parameterized types. The idea is to allow type (Integer, String, ⦠etc., and user-defined types) to be a parameter to methods, classes, and interfaces. It is possible to create classes that work with different data types using Generics. An entity such as a class, interface, or method
4 min read
Generics in Java Generics means parameterized types. The idea is to allow a type (like Integer, String, etc., or user-defined types) to be a parameter to methods, classes, and interfaces. Generics in Java allow us to create classes, interfaces, and methods where the type of the data is specified as a parameter. If w
10 min read
Restrictions on Generics in Java In Java, Generics are the parameterized types that allow us to create classes, interfaces, and methods in which the type of data they are dealing with will be specified as a parameter. This ensures the type safety at the compilation time. Syntaxclass class_name<T> { //Body of the class } Here,
5 min read
Generic Map In Java Java Arrays store items in an ordered collection and the values can be accessed using the index(an integer). Whereas HashMap stores as a Key/ Value pair. Using HashMap, we can store the items or values and these values can be accessed by indexes/ keys of any type be it Integer, String, Double, Chara
3 min read
Field getGenericType() method in Java with Examples The getGenericType() method of java.lang.reflect.Field used to return a Type object representing the declared type of this Field object. The returned type object can be one of the implementations of Type's subinterfaces: GenericArrayType, ParameterizedType, WildcardType, TypeVariable, Class. If the
2 min read