Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Generics with Bounded Types

Till now we have seen that generic type parameters could be replaced by any class type.

But what if we want to create a type parameter that could be replaced by a specific class type only & not with other types.

Is there any way to achieve this type of condition?

Yes, there is! With the help of bounded types parameters.

Bounded type parameters help to create a generic, classmethodinterface or constructor that can work only with specific class type & not with others.

Let’s consider an example for better understanding of this concept, as given below:

Java Programming Language Generics with Bounded Type

Here, we have created two classes Bus & Truck that extends abstract class TenWheeler.
We have also created a class Car that has no any relation with abstract class TenWheeler.

If we want to create a generic class that can work only with TenWheeler type and its subclasses, not with other types, then we can create this type of generic class with the help of bounded type parameter.

For that we just need to use the extends keyword while declaring the type parameter.

Example

<T extends TenWheeler>

T can be replaced by only TenWheeler or subclasses of TenWheeler.

Therefore general syntax to create bounded types is:

<T extends superclass>

T can be replaced by only superclass or its subclasses.

Example

 abstract class TenWheeler 
 { 
     String name;
	
     public abstract void start(); 
 }

 class Bus extends TenWheeler
 {
     Bus(String name)
     {
         this.name = name;	
     }
	
     @Override
     public void start()
     {
         System.out.println("Bus Started");	
     }
 }

 class Truck extends TenWheeler
 {
     Truck(String name)
     {
         this.name = name;   	
     }
	
     @Override
     public void start()
     {
         System.out.println("Truck Started");	
     }
 }

 class Car 
 {
     String name;
	
     Car(String name)
     {
         this.name = name;
     }
 }

 class BoundedDemo<T extends TenWheeler> 
 {
     T var;
	
     BoundedDemo(T v)
     {
         var = v;
     }
	
     public void vehicleName() 
     {
         System.out.println("Name of the Vehicle :: "+var.name);
     }
	
     public static void main(String args[]) 
     {
         Bus b = new Bus("Volvo");
         Truck t = new Truck("Mercedes-Benz");
         Car c = new Car("BMW");
		
         BoundedDemo<Bus> bus = new BoundedDemo<Bus>(b);
         BoundedDemo<Truck> truck = new BoundedDemo<Truck>(t);
         //BoundedDemo<Car> car = new BoundedDemo<Car>(c);
		
         bus.vehicleName();
         truck.vehicleName();
     }
 }

Output

Name of the Vehicle :: Volvo
Name of the Vehicle :: Mercedes-Benz

If we try to remove comments in the above program & try to recompile, we will get compile time error, because Car is not a subclass of a TenWheeler.

Compile error is as shown below:

Bound mismatch: The type Car is not a valid substitute for the bounded parameter <T extends TenWheeler> of the type BoundedDemo<T>

We can use bounded type parameters not only as per as our requirement but sometimes it becomes compulsion to use it.

Suppose we want to create a generic class named BoundedDemo that is going to call start() method of class Bus & Truck, through method check().
You might be thinking that it is a very easy task & will try to write a program in the way shown below:

 class BoundedDemo2<T> 
 {
     T var;
	
     BoundedDemo2(T v)
     {
         var = v;
     }
	
     public void check()
     {
         var.start();	
     }
	
     public static void main(String args[]) 
     {
         Bus b = new Bus("Volvo");
         Truck t = new Truck("Mercedes-Benz");
		
         BoundedDemo2<Bus> bus = new BoundedDemo2<Bus>(b);
         BoundedDemo2<Truck> truck = new BoundedDemo2<Truck>(t);
		
         bus.check();
         truck.check();
     }
 }

If we try to compile this program, we’ll get compile time error saying,

                     The method start() is undefined for the type T

Because Java compiler has no way to know that we want to create a generic class that is going to use only TenWheeler type & its subclasses & we can call start() method.

To avoid this problem, there must be some way to tell the compiler that we are going to perform operations only with TenWheeler type & its subclasses.

It can be done by bounded types.

Therefore using,

<T extends Tenwheeler>

instead of only <T>.

Now Java compiler comes to know that we want to perform operations only with TenWheeler type & its subclasses & they can call start() method.

Thus, henceforth compile error not received.

Let’s try

 class BoundedDemo2<T extends TenWheeler> 
 {
     T var;
	
     BoundedDemo2(T v)
     {
         var = v;
     }
	
     public void check()
     {
         var.start();	
     }
	
     public static void main(String args[]) 
     {
         Bus b = new Bus("Volvo");
         Truck t = new Truck("Mercedes-Benz");
		
         BoundedDemo2<Bus> bus = new BoundedDemo2<Bus>(b);
         BoundedDemo2<Truck> truck = new BoundedDemo2<Truck>(t);
		
         bus.check();
         truck.check();
     }
 }

Output

Bus Started
Truck Started

Bounded type can be defined in many forms like,

class GenericDemo<T extends superClass & Interface1 & Interface2>

Note

First, we must extend class & then implement the interface.

We can’t extend more than one class simultaneously.

We must use extend keyword to implement interfaces, not implements keyword.

 

Leave a reply