I’m going through example.
public interface Visitor<T> {
public void visit(T e);
}
class Sum<T extends Integer> implements Visitor<T> {
private int sum = 0;
@Override
public void visit(T element) {
sum += element;
}
public T value() {
return sum;
}
}
The return statement T value() return sum is giving error stating that Type mismatch: cannot convert from int to T. Why it is so and how to fix the problem.
In your case there are two problems:
T extends Integeris not exactly valid, becauseIntegercannot be extended, i.e. it’s afinalclass. Therefore you can safely change your class declaration toSecondly,
T value()method is not part of the interface and therefore does not need (should not) be of typeT. You can safely replace it withpublic int value()orpublic Integer value().Therefore the resulting code will look like this: