74

I am trying to define an abstract class implementing Comparable. When I define the class with following definition:

public abstract class MyClass implements Comparable <MyClass>

subclasses have to implement compareTo(MyClass object). Instead, I want every subclass to implement compareTo(SubClass object), accepting an object of its own type. When I try to define the abstract class with something like:

public abstract class MyClass implements Comparable <? extends MyClass>

It complains that "A supertype may not specify any wildcard."

Is there a solution?

0

7 Answers 7

53

It's a little too verbose in my opinion, but works:

public abstract class MyClass<T extends MyClass<T>> implements Comparable<T> {

}

public class SubClass extends MyClass<SubClass> {

    @Override
    public int compareTo(SubClass o) {
        // TODO Auto-generated method stub
        return 0;
    }

}
6
  • 3
    Consider (1) class MyImpl1 extends MyClass<MyImpl1> { ... }; and (2) class MyImpl2 extends MyClass<MyImpl1> { public int compareTo (MyImpl1 o ) {...} }. MyImpl2 is not doing the right thing.
    – emory
    Commented Aug 29, 2010 at 0:41
  • 2
    If we assume that every subclass extends MyClass with its own class as the generic parameter, the solution is correct. However, it seems that there is no way ensure this, as emory pointed out.
    – Cem
    Commented Aug 29, 2010 at 8:46
  • @emory This is also true for implementing Comparable directly. No one stops you from doing MyImpl2 implements Comparable<MyImpl1>. Commented Aug 29, 2010 at 12:49
  • 1
    But declaring the requirement forces sub classes to implement comparable instead of assuming it. Commented Aug 29, 2010 at 16:45
  • 3
    public abstract class MyClass<T> implements Comparable<T> is just as good
    – newacct
    Commented Nov 30, 2012 at 10:39
21

Apart from the mechanical difficulties you're encountering declaring the signatures, the goal doesn't make much sense. You're trying to establish a covariant comparison function, which breaks the whole idea of establishing an interface that derived classes can tailor.

If you define some subclass SubClass such that its instances can only be compared to other SubClass instances, then how does SubClass satisfy the contract defined by MyClass? Recall that MyClass is saying that it and any types derived from it can be compared against other MyClass instances. You're trying to make that not true for SubClass, which means that SubClass does not satisfy MyClass's contract: You cannot substitute SubClass for MyClass, because SubClass's requirements are stricter.

This problem centers on covariance and contravariance, and how they allow function signatures to change through type derivation. You can relax a requirement on an argument's type—accepting a wider type than the supertype's signature demands—and you can strengthen a requirement on a return type—promising to return a narrower type than the supertype's signature. Each of these freedoms still allows perfect substitution of the derived type for the supertype; a caller can't tell the difference when using the derived type through the supertype's interface, but a caller using the derived type concretely can take advantage of these freedoms.

Willi's answer teaches something about generic declarations, but I urge you to reconsider your goal before accepting the technique at the expense of semantics.

4
  • I agree with this answer. In addition, we should be coding to interfaces not classes. The actual implementation class may be an anonymous class, local class (nested inside a method), private (nested inside a class), or package private and thus not within our scope.
    – emory
    Commented Aug 29, 2010 at 0:46
  • 2
    Another problem I see is storing the subclass objects in a Collection. I should be able to store them with just List<MyClass>, instead of List<MyClass<?>>. And what would get called when you get one of those objects and call equals(anObject)?
    – TheLQ
    Commented Aug 29, 2010 at 0:59
  • seh, thanks for your answer. Actually, I am looking for a contract that will force every subclass to have a compareTo() method only for its own class, but not for any other subclass. My made-up generics definition might be misleading in this sense.
    – Cem
    Commented Aug 29, 2010 at 8:31
  • I understand, Cem, though it's still weird to force subclasses to have compareTo() defined. With Enum, it's providing that definition to derived types -- types that the compiler will generate for you -- and that's an unusual situation. In your case, you're not providing anything but an obligation for derived types. Is the intention to guarantee that you can write generic functions against type MyType<T> and be sure that it provides compareTo(MyType<T>)? If so, couldn't you also do that by having your function demand <T extends MyType && Comparable<T>>?.
    – seh
    Commented Aug 29, 2010 at 15:04
3

see Java's own example:

public abstract class Enum<E extends Enum<E>> implements Comparable<E>
    public final int compareTo(E o)

on seh's comment: usually the argument is correct. but generics makes type relations more complicated. a SubClass may not be a subtype of MyClass in Willi's solution....

SubClassA is a subtype of MyClass<SubClassA>, but not a subtype of MyClass<SubClassB>

type MyClass<X> defines a contract for compareTo(X) which all of its subtypes must honor. there is no problem there.

3
  • 1
    That's a good example, though it differs from Cem's original question. Now, I may have been reading his code too literally; perhaps this is exactly what he was trying to write. In this case, the Comparable facet of the Enum interface is about what a specific subclass can do with itself (or, rather, instances of itself), not what it can do with Enum-derived types in general. If that's what Cem was after, then I take your answer to be more appropriate than mine.
    – seh
    Commented Aug 29, 2010 at 2:56
  • I guess it is OK as long as all subclasses are a subtype of MyClass<?>. I am not sure, though, if it causes a problem in future steps. This is the first time I am designing with generics.
    – Cem
    Commented Aug 29, 2010 at 8:34
  • This is a bad example. Enum is a special case -- enum types are provided by the language, and have a specific form (an enum A will implement Enum<A>) which is not true of user-defined classes in general.
    – newacct
    Commented Nov 30, 2012 at 10:42
1

I'm not sure that you need the capture:

First, add the compareTo to the abstract class...

public abstract class MyClass implements Comparable <MyClass> {

@Override
public int compareTo(MyClass c) {
...
}    
}

Then add the implementations...

public class MyClass1 extends MyClass {
...
}

public class MyClass2 extends MyClass {
...
}

Calling compare will call the super type method...

MyClass1 c1 = new MyClass1();
MyClass2 c2 = new MyClass2();

c1.compareTo(c2);
1
  • Isn't this exactly the way Cem described his problem? How would you implement compareTo in MyClass1 or MyClass2 with a different parameter type? Commented Aug 29, 2010 at 0:13
1
public abstract class MyClass<T> implements Comparable<T> {

}

public class SubClass extends MyClass<SubClass> {

    @Override
    public int compareTo(SubClass o) {
        // TODO Auto-generated method stub
        return 0;
    }

}
6
  • This does not limit type parameter T to subclasses of MyClass. Commented Jul 21, 2014 at 9:39
  • @StevoSlavić: So? It is perfectly type-safe.
    – newacct
    Commented Jul 21, 2014 at 19:03
  • If I understood the original question/example correctly, one of the ideas was in restricting T to subclasses of MyClass, while compiler does not allow it. Commented Jul 21, 2014 at 21:25
  • @StevoSlavić: the question wants each subclass to be comparable to that subclass only
    – newacct
    Commented Jul 21, 2014 at 21:35
  • There's nothing preventing you from writing public class SubClass extends MyClass<WhateverClass> .... Commented Jul 21, 2014 at 22:52
1

Found another solution:

  1. Define an interface on the fields which make up the comaprable (e.g ComparableFoo)
  2. Implement the interface on the parent class
  3. Implement Comparable on the parent class.
  4. Write your implementation.

Solution should look like this:

public abstract class MyClass implements ComparableFoo,Comparable<ComparableFoo> {
    public int compareTo(ComparableFoo o) {
    // your implementation
    }
}

This solution implies that more things might implement ComparableFoo - this is likely not the case but then you're coding to an interface and the generics expression is simple.

1
  • I like this solution due to it's simplicity
    – Pehmolelu
    Commented Aug 24, 2015 at 6:14
0

I know you said you want "compareTo(SubClass object), accepting an object of its own type", but I still suggest declaring the abstract class like this:

public abstract class MyClass implements Comparable <Object>

and do an instanceof check when overriding compareTo in MySubClass:

@Override
public int compareTo(Object o) {
    if (o instanceof MySubClass)) {
        ...
    }
    else throw new IllegalArgumentException(...)
}

similarly to 'equals' or 'clone'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.