ordanj
ordanj

Reputation: 461

How to implement an interface with generic types?

I'm trying to write a class, TelemeterTester, which implements the Telemeter class. Telemeter extends Comparator. I keep getting this runtime error...

TelemeterTester.java:12: error: cannot find symbol
  return e1.compareTo(e2);
           ^
  symbol:   method compareTo(Comparable)
  location: variable e1 of type Comparable
  where Comparable is a type-variable:
  Comparable extends Object declared in class TelemeterTester
1 error  

Here is the code for Telemeter interface

/** @param <E> the type on which distance and order are defined
 *
 */
 public interface Telemeter<E> extends Comparator<E> {

   /**
    * Returns the distance between e1 and e2.
    *
    * @param e1 the first object
    * @param e2 the second object
    * @return the distance between e1 and e2
    *
    */
   double distance(E e1, E e2);
 }

Here is my code for TelemeterTester class, which implements Telemeter

public class TelemeterTester<Comparable> implements Telemeter<Comparable>{

   private TelemeterTester() {}

   public double distance(Comparable e1, Comparable e2) {
      return 0;
   }

   public int compare(Comparable e1, Comparable e2) {

      return e1.compareTo(e2);
   }

}

Can anyone explain to me what I'm doing wrong? I don't understand generics very well and I've been stuck on variations of this error for a few hours now.

Upvotes: 2

Views: 625

Answers (3)

Alexandre Santos
Alexandre Santos

Reputation: 8338

"Can anyone explain to me what I'm doing wrong?" Your tester is of type Comparable, which extends the Telemeter, which is also of the type Comparable.

What you really want is that the test extends a generic object, which we normally name E and you want this object to extend the class Comparable of Es. If you replace E with Integer, for example, it will make more sense:

public class TelemeterTester> implements Telemeter

"I don't understand generics very well and I've been stuck on variations of this error for a few hours now."

Josh Bloch has awesome videos on Youtube where he explains PECS :) It will make sense once you see them.

https://www.youtube.com/watch?v=V1vQf4qyMXg

Your code should look like this:

import java.util.*;

/** @param <E> the type on which distance and order are defined
*
*/
interface Telemeter<E> extends Comparator<E> {

  /**
   * Returns the distance between e1 and e2.
   *
   * @param e1 the first object
   * @param e2 the second object
   * @return the distance between e1 and e2
   *
   */
  double distance(E e1, E e2);
}

class TelemeterTester<E extends Comparable<E>> implements Telemeter<E> {

    private TelemeterTester() {}

    @Override
    public int compare(E o1, E o2)
    {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public double distance(E e1, E e2)
    {
        // TODO Auto-generated method stub
        return 0;
    }

 }

Replace the TODO with your own code.

Upvotes: 0

Shailesh Aswal
Shailesh Aswal

Reputation: 6802

Your TelemeterTester should be defined as below:

  public class TelemeterTester<E extends Comparable<E>> implements Telemeter<E> {
    private TelemeterTester() {
    }

    public int compare(E o1, E o2) {
        return o1.compareTo(o2);
    }

    @Override
    public double distance(E e1, E e2) {
        return 0;
    }
}

and referred as :

   TelemeterTester<Integer> t = new TelemeterTester<Integer>();
   System.out.println(t.compare(2, 2));

remember type Integer implements Comparable<Integer>

Upvotes: 2

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279930

The syntax here

public class TelemeterTester<Comparable> implements Telemeter<Comparable>{  

just like

public interface Telemeter<E> extends Comparator<E> {

declares a type parameter called Comparable, just like the second declares a type parameter called E. It doesn't use Comparable as a type argument. Therefore, this method

public int compare(Comparable e1, Comparable e2) {
    return e1.compareTo(e2);
}

uses an unbounded type for its parameters. An unbounded type is basically Object and Object does not declare a compareTo(..) method.

You should read

before you proceed.

Upvotes: 2

Related Questions