Reputation:
I got a interface like this:
public T evaluate(float fraction, T startValue, T endValue);
the other code will treat every T
type as an Object
, and finally I'll get the return value, which is an Object
class too.
What I want is to pass startValue
and endValue
as integer, but return a totally different instance (not an Integer
). I'll force cast the return value to what I supposed.
What I've done is to replace the T
with object, like this:
public Object evaluate(float fraction, Object startValue, Object endValue)
but this failed, I thought if the interface defined like ? extends T
will work, but it's not. (Am I right?)
So is there any way I can bypass the generics type check?
Upvotes: 0
Views: 613
Reputation: 471
I wrote this answer a long time ago when I didn't know much. Please consider the other answers.
I suppose you have an interface like interface Name<T>{}
.
Why not just let T
be Integer
and then cast what is returned to whatever you want. (if I wanted to get a string):
//interfaceInstance is Name<Integer>
//fraction is float, startValue and endValue are Integer
String value = String.valueOf(interfaceInstance.evaluate(fraction, startValue endValue))
Upvotes: 0
Reputation: 9658
Assuming your interface has a T generic parameter, add a second type, say R:
interface MyInterface<T,R> {
public R evaluate(float fraction, T startValue, T endValue);
}
Upvotes: 4