How to implement an interface with generic types?

ordanj

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.

Shail016

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to implement an interface with generic types?

From Dev

How to implement a generic interface with a child generic interface

From Dev

How to implement a generic interface with a child generic interface

From Dev

How to implement generic types with currying

From Dev

How implement an interface Generic Method

From Dev

How implement an interface Generic Method

From Dev

limit T in a generic class to types that implement some interface

From Dev

How do I implement a generic class that has an constraint for a generic interface?

From Dev

How to implement a generic interface which implements another interface?

From Dev

implement generic generic interface in scala

From Dev

Implement Interface Generic Function

From Dev

Implement a generic interface

From Dev

Implement Interface Generic Function

From Dev

Generic Interface with different types

From Dev

How do you implement specific types on generic traits in rust?

From Dev

How to implement generic GetById() where Id can be of various types

From Dev

How to implement generic function that receives differents types scala

From Dev

Java how to implement interface with a variadic method and generic return type

From Dev

How can I implement a generic interface in Rhino JS?

From Dev

How to just get the method in the implement class with a generic interface in Java

From Dev

Dictionary of classes that implement a generic interface

From Dev

Dictionary of classes that implement a generic interface

From Dev

Get implemented types of generic interface

From Java

Getting all types that implement an interface

From Dev

Implement a generic C++/CLI interface in a C# generic interface

From Dev

Implement a generic C++/CLI interface in a C# generic interface

From Dev

How to implement a generic encoder?

From Dev

Can you implement a generic struct for multiple types?

From Dev

Implement Same Function for different Generic Types with Reflection

Related Related

  1. 1

    How to implement an interface with generic types?

  2. 2

    How to implement a generic interface with a child generic interface

  3. 3

    How to implement a generic interface with a child generic interface

  4. 4

    How to implement generic types with currying

  5. 5

    How implement an interface Generic Method

  6. 6

    How implement an interface Generic Method

  7. 7

    limit T in a generic class to types that implement some interface

  8. 8

    How do I implement a generic class that has an constraint for a generic interface?

  9. 9

    How to implement a generic interface which implements another interface?

  10. 10

    implement generic generic interface in scala

  11. 11

    Implement Interface Generic Function

  12. 12

    Implement a generic interface

  13. 13

    Implement Interface Generic Function

  14. 14

    Generic Interface with different types

  15. 15

    How do you implement specific types on generic traits in rust?

  16. 16

    How to implement generic GetById() where Id can be of various types

  17. 17

    How to implement generic function that receives differents types scala

  18. 18

    Java how to implement interface with a variadic method and generic return type

  19. 19

    How can I implement a generic interface in Rhino JS?

  20. 20

    How to just get the method in the implement class with a generic interface in Java

  21. 21

    Dictionary of classes that implement a generic interface

  22. 22

    Dictionary of classes that implement a generic interface

  23. 23

    Get implemented types of generic interface

  24. 24

    Getting all types that implement an interface

  25. 25

    Implement a generic C++/CLI interface in a C# generic interface

  26. 26

    Implement a generic C++/CLI interface in a C# generic interface

  27. 27

    How to implement a generic encoder?

  28. 28

    Can you implement a generic struct for multiple types?

  29. 29

    Implement Same Function for different Generic Types with Reflection

HotTag

Archive