If i declare a generic class as something like
public class Driver<V extends Car>
where Car is an interface.
Then, I use it with something like this:
Driver<?> driver = new Driver<Chevrolet>();
I don’t want to specify a specific implementation of car as the generic.
Why is it that I cannot call methods implemented in driver that uses the generic class as the parameter?
For example if Driver has a method like
public void drive(V vehicle)
It does not allow me to call that with my instance of driver (Driver<?>).
Because the compiler doesn’t know what type of argument
driver.drive(? vehicle)will accept. Should it take a Chevrolet, a Honda, or some other type?For more details you might find Gilad Bracha’s Generics Tutorial helpful.
What is the purpose of your drive() method. Does the
vehicleparameter need to be typed to the specific subtype of Car? Would it be more appropriate for it to simply accept aCar?The following was part of my original answer, but discussion in the comments proved that it was incorrect. I’m leaving it here so the comments aren’t orphaned.
Try declaring your method like this:
and see if that works better for you.