generic interfaces with generic methods

M

Murat Tasan

i have a generic interface defined as:

public interface DistanceFunction<S, T>
{
public <D extends Number & Comparable<D>> D computeDistance(S source, T
trget);
}

and this compiles just fine. note how i want to return a Number that is
guaranteed to be comparable to itself.

now i am trying to implement this with a simple example:

public class TestDistanceFunction implements DistanceFunction<Object,
Object> {
public Double computeDistance(Object source, Object target) {
return new Double(69.0d);
}
}

everything should be fine at this point i think. in fact it compiles just
fine. the generic type variables used in the interface (S and T) are
correct and give no problem. but when i compile with all warnings on, i
get the following warning:

sourcepath/TestDistanceFunction.java:3: warning:
computeDistance(java.lang.Object,java.lang.Object) in TestDistanceFunction
implements <D>computeDistance(S,T) in mur.math.DistanceFunction; return
type requires unchecked conversion found : java.lang.Double
required: D
public Double computeDistance(Object x, Object y)
^
1 warning

now, all i want is for any implementing class of the DistanceFunction
interface to return a Number that is comparable to itself. Double
certainly satisfies this. using D in the generic method declaration
simplifies the generic interface (i.e. leaving D out of the generic
interface type variables), but it is only used to indicate the range of
possible return values from the method.

does anyone have any elegant ideas as to how to get rid of this warning?
note that "elegant" means NOT including D as one of the generic interface
type variables, as its only purpose is a place-holder for the return type
of that single method.

thanks much for any insight,

murat
 
Joined
Feb 3, 2009
Messages
1
Reaction score
0
I think to implement a Generic interface your class must also be a Generic class.Even thought your class has nothing to do with those parameter type you need a Generic class to implement the generic interface.Because there must me some means to pass the paramater types to tine interface you are implementing.So your implementing class should accpet the paramaeter types to passs it to the interface.

I think it looks like

public class TestDistanceFunction<S,T> implements DistanceFunction<S,T>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top