I have this interface:
public interface Inflatable {
Pump<? extends Inflatable> getPump();
}
and this interface:
public Pump<T extends Inflatable> {
int readPressure(T thingToInflate);
}
Now this class:
public class Preparer {
public <T extends Inflatable> void inflate(T thingToInflate) {
int pressure = thingToInflate.getPump().readPressure(thingToInflate);
}
}
does not compile, with this error:
The method readPressure(capture#1-of ? extends Inflatable) in the type Pump is not applicable for the arguments (T)
What is wrong here? The variable thingToInflate
has to be an instance of a subclass of Inflatable
(because of <T extends Inflatable>
, right?), and the readPressure
method is defined to require a subclass of Inflatable
.
I know that this particular example is contrived, but the general case is that given an instance of T
, I can't then pass that instance to a method in another class that appears to define T
in exactly the same way. Can I fix this?