Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > generic interfaces with generic methods

Reply
Thread Tools

generic interfaces with generic methods

 
 
Murat Tasan
Guest
Posts: n/a
 
      06-02-2005
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
 
Reply With Quote
 
 
 
 
Chaitanya Chaitanya is offline
Junior Member
Join Date: Feb 2009
Posts: 1
 
      02-03-2009
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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why only public methods on interfaces? kramer31 Java 17 04-10-2011 05:10 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Methods in ExecutorService and Excutor interfaces ankur Java 4 12-20-2007 01:26 AM
static methods in interfaces ballpointpenthief Java 23 06-13-2007 01:30 PM
Interfaces and generic templates Christopher Benson-Manica C++ 1 02-24-2004 03:16 PM



Advertisments