The wildcard operator and super keyword.

Hi,
I was reading below link on
http://www.thejavageek.com/2013/08/28/generics-the-wildcard-operator/

i have not understand below

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.

also

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

what it means by we cannot pass collection. when to use super along with ?.

please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kanti PrasadCommented:
Hi

Please read this as it will clear your doubts and if you still cannot then please let me know.

http://tutorials.jenkov.com/java-generics/wildcards.html

The Key you need to understand is that Car and Byke inherit Vehicles class. So when you are using wild cards to put Vehicle Class and inherited class in a collection the compiler ensures that you don't put in Car into Byke or Byke into Car as both are subclasses of Vehicle.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gangadhara MelukoteCommented:
1)
Vehicle is super class of Bike.
Vehicle is super class of Car.
The 'List<? super Bike>' meaning is: You can add any super class of Bike. Since Car is not in hierarchy structure of Bike, you cannot add car object to Bike hierarchy list.

2) As it is clearly mentioned.

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>.

 The List<? extends Vehicle> can accept all kind of list of classes that are extending 'Vehicle' class. Compiler blindly accepts the list of Car at the time of adding, but this will cause type cast exception at run time.
gudii9Author Commented:
The List<? extends Vehicle> can accept all kind of list of classes that are extending 'Vehicle' class. Compiler blindly accepts the list of Car at the time of adding, but this will cause type cast exception at run time



The 'List<? super Bike>' meaning is: You can add any super class of Bike. Since Car is not in hierarchy structure of Bike, you cannot add car object to Bike hierarchy list.

above lines makes more sense to me. Any good examples explaining difference between

List<? extends Vehicle>

and

List<? super Bike>

please advise
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

gudii9Author Commented:
package com.thejavageek.generics;

import java.util.ArrayList;
import java.util.List;

public class Mechanic {

    public void addVehicle(List<Vehicle> vehicles){
        // some code here
    }

    public static void main(String[] args) {

        List<Vehicle> vehicles = new ArrayList<Vehicle>();
        vehicles.add(new Vehicle());
        vehicles.add(new Vehicle());

        List<Bike> bikes = new ArrayList<Bike>();
        bikes.add(new Bike());
        bikes.add(new Bike());

        List<Car> cars = new ArrayList<Car>();
        cars.add(new Car());
        cars.add(new Car());

        Mechanic mechanic = new Mechanic();

        mechanic.addVehicle(vehicles); // compiles fine
        mechanic.addVehicle(bikes);    // compilation fails
        mechanic.addVehicle(cars);     // compilation fails

    }

}

Open in new window


i tried above and seeing lot of errors. please advise
gudii9Author Commented:
The List<? extends Vehicle> can accept all kind of list of classes that are extending 'Vehicle' class. Compiler blindly accepts the list of Car at the time of adding, but this will cause type cast exception at run time.


above is bit unclear
Why this will cause type cast exception at run time?
please advise
gudii9Author Commented:
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>.

i was not clear on above also from the link. Please advise
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.