[ACCEPTED]-Abstract Generic class-generics
Accepted answer
public abstract class Step<S,T> {
public abstract S makeAStep(S currentResult, T element);
}
public class SumOfInts extends Step<Integer,Integer> {
// etc.
0
I agree with Jonathan's answer.
There is 9 also another possibility, given below, that 8 keeps the type parameters on the method itself.
It's only theory in this case, because 7 the class and method names suggest that 6 this has no meaning for this example.
So 5 I change the names for my example:
public abstract class Step {
public abstract <S,T> String makeAStep(S first, T second);
}
public class ConcatTwo extends Step {
public <S, T> String makeAStep(S first, T second){
return String.valueOf(first) + String.valueOf(second);
}
}
Note : This 4 works because the operation uses
String.valueOf(Object)
, that 3 works for any type (all subclass Object). For 2 another operation, we would have to restrict 1 S and T, using something like
S extend Integer
for example.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.