generic interfaces with generic methods

Discussion in 'Java' started by Murat Tasan, Jun 3, 2005.

  1. Murat Tasan

    Murat Tasan Guest

    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
    Murat Tasan, Jun 3, 2005
    #1
    1. Advertising

  2. Murat Tasan

    Chaitanya

    Joined:
    Feb 3, 2009
    Messages:
    1
    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>
    Chaitanya, Feb 3, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher Benson-Manica

    Interfaces and generic templates

    Christopher Benson-Manica, Feb 24, 2004, in forum: C++
    Replies:
    1
    Views:
    357
    Christopher Benson-Manica
    Feb 24, 2004
  2. ballpointpenthief

    static methods in interfaces

    ballpointpenthief, Dec 7, 2006, in forum: Java
    Replies:
    23
    Views:
    861
  3. ankur
    Replies:
    4
    Views:
    1,011
    ankur
    Dec 20, 2007
  4. kramer31
    Replies:
    17
    Views:
    982
    Tom Anderson
    Apr 10, 2011
  5. Kenneth McDonald
    Replies:
    5
    Views:
    301
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page