Generics: The wildcard operator

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

In the previous part of this tutorial, Polymorphism with generics we have learned the shortcomings of generics while using polymorphism with it.In this part, we are going to overcome them using wildcard operator.

Please read this article before continuing.

Vehicle Class

Bike Class

Car class

Mechanic Class

Now we want to be able to pass a ArrayList<Bike> to addVehicle(List<Vehicle> vehicles) successfully but due to type erasure, as discussed in previous article, it is not possible. Here comes the use of wildcard operator i.e. ? .

Wildcard operator and extends keyword

We will change the method to

After doing this,

So what does it mean ?

The statement  List<? extends Vehicle> means that you can pass any type of list that subtype of List and which is typed as Vehicle or some subtype of Vehicle. But you cannot add anything into the collection at all. 

Now what is that? Why can’t we add anything into the collection? Lets consider this.

The compiler stops you at the very moment. It doesn’t allow you to add anything while using ? extends. Why is that?

  • ? extends allows us to pass any collection that is subytype of the method parameter which is typed as generic type of subtype of the generic type. i.e. we can pass an  ArrayList<Vehicle> , ArrayList<Bike> or ArrayList<Car> to it.
  • But consider a scenario in which we are passing ArrayList<Bike> to addVehicle(List<? extends Vehicle>). In that case, if compiler didn’t stop us from adding into the collection, we would have added a Car into ArrayList<Bike>.

This is the scenario so ? extends doesn’t allow you to add into collection. But there is also a workaround for this. We can also pass a subtype collection and still be able to add into collection. We have to use the super keyword along with wildcard operator

The wildcard operator and super keyword.

Now change as follows

and we call this method using

Now what is this?

What does List<? super Bike> mean?

You can pass any collection that is of type Bike or any super type of Bike. i.e. Vehicle. and you can add elements into it. So Even if you pass a list of bikes or list of vehicles, you will still be able to add a Bike into it. But you cannot add a Car into the collection because our method parameter declaration allows only a list of bikes or list of supertype of Bike, i.e. Vehicle.


Hope this articles helps you understand the wildcard operator and use of extends and super keyword with it.

Share Button

13 comments for “Generics: The wildcard operator

  1. Smita
    September 11, 2013 at 11:11 am

    Nice.. this is really useful and thorough!

  2. Abhijit Mehetre
    June 10, 2014 at 12:34 pm

    Got it……

    • June 10, 2014 at 2:45 pm

      I am glad it was useful for you 🙂

      • Febri
        July 25, 2014 at 8:49 pm

        This is also very Useful for me as you implemented it into conrete examples ! Thanks.

        • July 25, 2014 at 9:37 pm

          Hi Febri, thanks for stopping by and commenting. I am glad it was useful for you. Happy learning 🙂

  3. rizwan
    October 1, 2014 at 7:57 pm

    nice ………………… sir

  4. Chetan Koli
    January 15, 2015 at 12:44 pm

    Thanks for explanation but I have one doubt,

    You said in bold that “But you cannot add a Car into the collection because our method parameter declaration allows only a list of bikes or list of supertype of Bike, i.e. Vehicle.”

    but in code
    vehicles.add(new Bike()); // compiles fine because we are adding a Bike
    vehicles.add(new Vehicle());// can’t add a Vehicle to a list of Bike

    why, here you con’t add Vehicle to list of Bike??? Contradictory……plz explain…

    • January 15, 2015 at 11:00 pm

      Hi Chetan, I think you are getting confused between “Adding an object into collection” and “Passing a list of objects to method”.
      Suppose you have something like this List vehicles = new ArrayList();
      now you are trying to call mechanic.addVehicle(vehicles); Now this is possible because of method parameter uses ‘? super Bike’. So you can pass a list of any type that is of type Bike or super type of it. Now, consider you are passing a list of ‘Bike’ So that list will contain only the objects of type ‘Bike’ and it will not allyouow to have supertype elements i.e. ‘Vehicle’ elements in collection. (Can you add an ‘Object’ to a list of ‘String’? (Similar condition)). A superclass object cannot be added into a collection which is meant to hold subclass objects.

      Remember that passing a collection to method is different (where passing list of vehicles is possible because it is super type of bike) and adding objects into collection is different (You cannot add a supertype object into a collection of type subtype).

      I hope this clears your doubt. 🙂

      • Chetan Koli
        January 18, 2015 at 1:04 am

        Thank you for clear explanation… 😀

  5. Anushree
    May 17, 2015 at 6:57 pm

    Hi Prasad,

    Your articles are very helpful for building my core concepts. I have one doubt, can we have a method where we can pass all the three types i.e Vehicle, Bike and Car? If yes, can you share the method signature.

    Thanks & Regards

    • May 18, 2015 at 11:56 am

      Hi Anushree, the same doubt that you’ve asked is explained in the article. If you write < ? extends Vehicle> , then you can pass all 3 types to it.

  6. Hassan
    June 5, 2015 at 10:02 am

    Nice Work brother. This is AWESOME….really Awesome…..And fully cleared my concept

Leave a Reply

Your email address will not be published. Required fields are marked *