Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Generics with WildCard Arguments

In the game of cards, the wild card can replace any other card or can be matched with any other card to form a pair.

For example, generally, we consider Joker card as a wild card. Therefore it can replace any other card or can be matched with any card to form a pair.

Java Programming Language Generics using WildCard Arguments

Similarly, in Java, generics use a concept known as a wildcard which can match with any reference type among valid types.

It is denoted by the sign ?.

Why wildcard?

To understand where and why wildcard must be used in Generics, let’s take a small example.
Consider a generic class WCDemo which uses bounded type parameter that extends TenWheeler. Therefore it can be used with TenWheeler & its subclasses Bus & Truck.
Suppose we want to pass the Truck type to the check() method invoked by Bus type.
Then you might think it a very simple task & write a program as shown below:

 abstract class TenWheeler 
 {
     String name;
	
     public abstract String vehicleName(); 
 }

 class Bus extends TenWheeler
 {
     Bus(String name)
     {
         this.name = name;	
     }
	
     @Override
     public String vehicleName() 
     {
         return name;
     }
 }

 class Truck extends TenWheeler
 {
     Truck(String name)
     {
         this.name = name;   	
     }
	
     @Override
     public String vehicleName() 
     {
         return name;
     }
 }

 class WCdemo<T extends TenWheeler> 
 {
     T var;
	
     WCdemo(T v)
     {
         var = v;
     }
	
     public void show(WCdemo<T> ob)
     {
         String bn = var.vehicleName();
         String tn = ob.var.vehicleName();
         System.out.println("Bus Name :: "+bn);
         System.out.println("Truck Name :: "+tn);
     }
	
     public static void main(String args[]) 
     {
         Bus b = new Bus("Volvo");
         Truck t = new Truck("Mercedes-Benz");
		
         WCdemo<Bus> bus = new WCdemo<Bus>(b);
         WCdemo<Truck> truck = new WCdemo<Truck>(t);
		
         bus.show(truck);
     }
 }

When we attempt to compile this program, we get compile time error shown below:

The method show(WCdemo<Bus>) in the type WCdemo<Bus> is not applicable for the arguments (WCdemo<Truck>)

This happens because invoking object for method show() is bus which is of Bus type.

Therefore compiler automatically replaces <T> in show() method with <Bus>.

Therefore show() method expect the Bus value as an argument to it.

But from the main() method, we are passing a value truck of type Truck.

This results in a mismatch between the parameter type & argument type.

Therefore we receive compile time error.

To solve this type of problem, we can make use of a wildcard (?) in place of T in show() method.

Let’s try

 class WCdemo<T extends TenWheeler> 
 {
     T var;
	
     WCdemo(T v)
     {
         var = v;
     }
	
     public void show(WCdemo<?> ob)         // used wildcard argument
     {
         String bn = var.vehicleName();
         String tn = ob.var.vehicleName();
         System.out.println("Bus Name :: "+bn);
         System.out.println("Truck Name :: "+tn);
     }
	
     public static void main(String args[]) 
     {
         Bus b = new Bus("Volvo");
         Truck t = new Truck("Mercedes-Benz");
		
         WCdemo<Bus> bus = new WCdemo<Bus>(b);
         WCdemo<Truck> truck = new WCdemo<Truck>(t);
		
         bus.show(truck);
     }
 }

Output

Bus Name :: Volvo
Truck Name :: Mercedes-Benz

Now, this program compiles & Runs fine due to use of wildcard argument.

We know that wildcard ‘?’ can match any type because Java considers it as an unknown type.

When the Truck type is passed to method check(), it accepts without any error, regardless of the type that invokes this method.

Therefore we can conclude that wildcard matches with any valid object type.

 

Leave a reply