Wildcards in generics

  • Eclipse: Oxygen
  • Java: 1.8

This wildcard in generics is basically the question mark (?)That is used in Java. The use of this can be done in various ways. The wildcard can be used in a variety of situations, such as the type of parameter, field, or local variable; sometimes as a return type.  In addition, you see the use of this in the case of inheritance.

The unknown Wildcard

The question mark (?), Represents the wildcard character and represents an unknown type in generics. As we do not know what type of List is typed, we can only read from the collection, and we can only treat the objects read as being Object instances.

In the following program, each class has an object as a superclass. Hence we can add several types of objects. List which we are printing in the display method is keep-able to handling multiple types of object. As we do not know the object hence we type List<?> it means a list typed to an unknown type. This could be a List<Vehicle>, a List<Car>, a List<String>etc.

Output

Vehicle [VehicleId=10]

Vehicle [VehicleId=11]

Vehicle [VehicleId=12]

Vehicle [VehicleId=13]

Car [CarModel=A14]

Upper Bounded Wildcards

These wildcards can be used when you want to restrictions on the unknown type to be a specific type or a subtype of that type. To declare upper-bounded wildcard, we use the wildcard character (<?>), Followed by the extend keyword.

public static void display(List<? extends Vehicle> list

In the following program, we want to restrict the vehicle type of object that is allowed to be passed to a type parameter. Therefore, the upper bounded wildcard is used.

When we apply the upper bound, it means list would be accepting an object of vehicle or child object of the vehicle.

Output

Vehicle [VehicleId=10]

Vehicle [VehicleId=11]

Vehicle [VehicleId=12]

Vehicle [VehicleId=13]

Car [CarModel=A14]

Lower Bounded Wildcards

A lower bounded wildcard restricts the unknown type to be a specific type or a supertype of that type. To declare lower-bounded wildcard, we use the wildcard character (<?>), followed by the keyword super.

List<? Super Car>

In the following program, List<? super Car> means that the list is typed to either the Car class or a superclass of Car.

Output

Vehicle [VehicleId=10]

Vehicle [VehicleId=11]

Vehicle [VehicleId=12]

Vehicle [VehicleId=13]

Car [CarModel=A14]

Let’s see one more example.

In this program polymorphism application has been applied and dynamically we are able to pick up the correct version of info inside the “for loop”.

We are able to pick up the correct version when the object is of car type and the object is of vehicle type.

Output

vehicle ID is 10

vehicle ID is 11

vehicle ID is 12

vehicle ID is 13

Car Model is A14

We cannot call the info() method in lower bound, it is a restriction.

Output:

Compile time error:

The method info() is undefined for the type object.

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu