Why is it not possible to implement Comparable<T> multiple times?

Niklas R

It seems like you usually implemented the java.lang.Comparable interface without specifying the type parameter.

public abstract class Area implements Comparable {
    @Override
    public int compareTo(Object other) {
        if (other instanceof Area)
            return new Double(getArea()).compareTo(other.getArea());
        return -1; // or something else
    }
    abstract public double getArea();
}

Since I only want to compare apples with apples, I think it would make sense to specify the type.

public abstract class Area implements Comparable<Area> {
    @Override
    public int compareTo(Area other) {
        // ...

If I want to introduce another class to compare Area with, I thought I could do the following:

public abstract class Area implements Comparable<Area>, Comparable<Volume> {
    @Override
    public int compareTo(Area other) {
        // ...
    }
    @Override
    public int compareTo(Volume other) {
        // ...
    }
}

But the java compiler tells me:

Area.java:2: error: repeated interface
public abstract class Area implements Comparable<Area>, Comparable<Volume> {
                                                                   ^
Area.java:2: error: Comparable cannot be inherited with different arguments: <Area> and <Volume>
  1. Are there any drawbacks specifying the type argument for the generic interface?
  2. Why won't Java allow me this multiple implementation?

Note: I'm using Java version 1.7.0_45

user719662
  1. No, it's not a drawback of specifying the generic - it's actually a feature. Also, I don't recall any drawback for using generics in interfaces, other than the well-known fact you can't instantiate a generic type nor create a generic array (but that's more a problem of implementation, not the interface itself).

  2. It's due to type erasure. Comparable<Area> and Comparable<Volume> is essentially the same class for the VM, and, shortly after checking validity, also for compiler.

If you want to have two distinct comparable interfaces implemented, just use Comparators for them - it's generally easier to maintain composition than inheritance in classes.

For some applications (distinguishing generics at run-time) you may also try subclassing them, e.g. ComparableArea extends Comparable<Area> & ComparableVolume extends Comparable<Volume>, but that would, in this particular case, cause more trouble than it would solve IMO, since you'd still get Comparable cannot be inherited with different arguments error - but at least you could differentiate those interfaces by e.g. instanceof.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is it impossible to implement a generic interface multiple times with different type parameters?

From Dev

Why can't I implement multiple interfaces?

From Dev

Why Can't We Initialize A Structure Multiple Times in C?

From Dev

Why can't I import a variable multiple times?

From Dev

Why can't I use clSetUserEventStatus() multiple times?

From Dev

Why LocalDate doesn't implements Comparable<LocalDate>?

From Dev

Why can't I implement a parametrized interface mutiple times with different parameters?

From Dev

Why does T extends Comparable <? super T> include T? Meaning includes Comparable<T>?

From Dev

Is it possible to implement `Functor<T>` in Java?

From Dev

Why is this function called multiple times?

From Dev

Why are didBeginContact called multiple times?

From Dev

Why is viewDidLayoutSubviews called multiple times?

From Dev

Why are didBeginContact called multiple times?

From Dev

How to implement Comparable?

From Dev

Implement Comparable in Java

From Dev

Implement an interface that extends comparable

From Dev

How to implement Comparable in KotlinPoet?

From Java

Why can't I use Docker CMD multiple times to run multiple services?

From Dev

Generics in Java - Why I am not allowed to do Comparable<? extends T> instead of Comparable<? super T>?

From Dev

Why generic constraint T extends Comparable<T> is not sufficient to avoid a cast?

From Dev

Comparable<T> vs Raw Comparable

From Dev

Why don't set methods in Scala enforce comparable types?

From Dev

Why can't we use only comparable in every situation?

From Dev

Why I can redefine the same variable multiple times in a for loop but can't outside of a loop?

From Dev

Why can't I filter tail's output multiple times through pipes?

From Dev

Why won't getline function work multiple times in a for loop with an array of structures?

From Dev

Why doesn't <Table/> show the check box when rendered multiple times?

From Dev

In Python regexes, why can't I match 0-or-more, multiple times

From Dev

Why can't I filter tail's output multiple times through pipes?

Related Related

  1. 1

    Why is it impossible to implement a generic interface multiple times with different type parameters?

  2. 2

    Why can't I implement multiple interfaces?

  3. 3

    Why Can't We Initialize A Structure Multiple Times in C?

  4. 4

    Why can't I import a variable multiple times?

  5. 5

    Why can't I use clSetUserEventStatus() multiple times?

  6. 6

    Why LocalDate doesn't implements Comparable<LocalDate>?

  7. 7

    Why can't I implement a parametrized interface mutiple times with different parameters?

  8. 8

    Why does T extends Comparable <? super T> include T? Meaning includes Comparable<T>?

  9. 9

    Is it possible to implement `Functor<T>` in Java?

  10. 10

    Why is this function called multiple times?

  11. 11

    Why are didBeginContact called multiple times?

  12. 12

    Why is viewDidLayoutSubviews called multiple times?

  13. 13

    Why are didBeginContact called multiple times?

  14. 14

    How to implement Comparable?

  15. 15

    Implement Comparable in Java

  16. 16

    Implement an interface that extends comparable

  17. 17

    How to implement Comparable in KotlinPoet?

  18. 18

    Why can't I use Docker CMD multiple times to run multiple services?

  19. 19

    Generics in Java - Why I am not allowed to do Comparable<? extends T> instead of Comparable<? super T>?

  20. 20

    Why generic constraint T extends Comparable<T> is not sufficient to avoid a cast?

  21. 21

    Comparable<T> vs Raw Comparable

  22. 22

    Why don't set methods in Scala enforce comparable types?

  23. 23

    Why can't we use only comparable in every situation?

  24. 24

    Why I can redefine the same variable multiple times in a for loop but can't outside of a loop?

  25. 25

    Why can't I filter tail's output multiple times through pipes?

  26. 26

    Why won't getline function work multiple times in a for loop with an array of structures?

  27. 27

    Why doesn't <Table/> show the check box when rendered multiple times?

  28. 28

    In Python regexes, why can't I match 0-or-more, multiple times

  29. 29

    Why can't I filter tail's output multiple times through pipes?

HotTag

Archive