Bounded type parameters in Java generics restrict the types that can be used as type arguments. They are useful when generic code needs to operate on a specific type or its subtypes.
- Allow access to methods defined by the upper bound without explicit type casting.
- Prevent incompatible type arguments at compile time.
Example: Program to demonstrates how a bounded type parameter restricts type arguments to a class and its subclasses.
class A{
void displayClass(){
System.out.println("Class A");
}
}
class B extends A{
@Override
void displayClass(){
System.out.println("Class B");
}
}
class C extends A{
@Override
void displayClass(){
System.out.println("Class C");
}
}
// Generic class with an upper bound
class Bound<T extends A>{
private T obj;
Bound(T obj) {
this.obj = obj;
}
void display() {
obj.displayClass();
}
}
public class GFG{
public static void main(String[] args){
Bound<A> a = new Bound<>(new A());
Bound<B> b = new Bound<>(new B());
Bound<C> c = new Bound<>(new C());
a.display();
b.display();
c.display();
}
}
Output
Class A Class B Class C
Explanation:
- T extends A specifies A as the upper bound.
- Bound<A>, Bound<B>, and Bound<C> are valid because the bound accepts A and its subclasses.
- The display() method can access displayClass() because it is defined in the upper bound A.
Declaration of Bounded Type Parameter
A bounded type parameter is declared using the extends keyword, followed by a class or interface that specifies its upper bound.
Syntax:
<T extends Type>
Here:
- T represents the type parameter.
- extends specifies the upper bound, whether it is a class or an interface.
- Type represents the permitted type and its subtypes.
Using an Invalid Type Argument
If a type argument does not satisfy the specified bound, the compiler reports an error.
Consider the following statement using the previously defined Bound class:
Bound<String> obj = new Bound<>("Java");
Compilation Error:
error: type argument String is not within bounds of type-variable T
The error occurs because String is neither A nor a subclass of A.
Multiple Bounds
Multiple bounds allow a type parameter to satisfy more than one type constraint. The bounds are separated using the & symbol.
- A type parameter can have at most one class bound, which must appear first.
- A class bound is optional; multiple bounds can consist entirely of interfaces.
- The type argument must satisfy all specified bounds.
Syntax:
// Using a class and interfaces
<T extends ClassName & Interface1 & Interface2>// Using only interfaces
<T extends Interface1 & Interface2>
class A{
public void display(){
System.out.println("Method of class A");
}
}
interface B{
void show();
}
// Satisfies both bounds
class C extends A implements B{
@Override
public void show(){
System.out.println("Method of interface B");
}
}
// Generic class with multiple bounds
class Bound<T extends A & B>{
private T obj;
Bound(T obj){
this.obj = obj;
}
public void execute(){
obj.display();
obj.show();
}
}
public class Geeks{
public static void main(String[] args){
Bound<C> obj = new Bound<>(new C());
obj.execute();
}
}
Output
Method of class A Method of interface B
Explanation:
- T extends A & B requires the type argument to satisfy both bounds.
- Class C extends A and implements interface B, making it a valid type argument.
- The execute() method can access methods from both bounds.