What is wrong with Class A
below that won't allow it to compile?
public class GenericsHell {
interface Shape{}
interface Circle extends Shape {}
interface ShapeHelper<T extends Shape> {
void draw(T shape);
}
class A<T extends Shape> {
A(T shape, ShapeHelper<? extends Shape> helper) {
helper.draw(shape); // Issues not-applicable argument error
}
}
class B {
B(Circle circle, ShapeHelper<Circle> helper) {
helper.draw(circle);
}
}
}
Eclipse gives the following error:
The method draw(capture#1-of ? extends Shape) in the type ShapeHelper<capture#1-of ? extends Shape> is not applicable for the arguments (T)
The problem is that in your declaration, shape is of type T, but you request a ShapeHelper of type <? extends Shape> which means that one could pass as argument a ShapeHelper where S and T are distinct.
You would then call
helper<S>.draw(shape<T>);
which doesn't make sense.A correct implementation for this method would be:
Which ensures that the shape and the shape helper are of compatible types.