Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   generic interfaces with generic methods (http://www.velocityreviews.com/forums/t143938-generic-interfaces-with-generic-methods.html)

Murat Tasan 06-02-2005 11:05 PM

generic interfaces with generic methods
 
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

Chaitanya 02-03-2009 12:17 PM

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>


All times are GMT. The time now is 12:55 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.