views:

365

answers:

3

I'm upgrading some code to Java 5 and am clearly not understanding something with Generics. I have other classes which implement Comparable once, which I've been able to implement. But now I've got a class which, due to inheritance, ends up trying to implement Comparable for 2 types. Here's my situation:

I've got the following classes/interfaces:

interface Foo extends Comparable<Foo>

interface Bar extends Comparable<Bar>

abstract class BarDescription implements Bar

class FooBar extends BarDescription implements Foo

With this, I get the error 'interface Comparable cannot be implemented more than once with different arguments...'

Why can't I have a compareTo(Foo foo) implemented in FooBar, and also a compareTo(Bar) implemented in BarDescription? Isn't this simply method overloading?

Edit: I have many classes which extend BarDescription. If I remove the type parameter for Comparable on Bar, leaving it in the raw state, then I get a bunch of compiler warnings when sorting all the classes which extend BarDescription. Would this be solved with the wildcards answer below? That answer looks quite complicated and difficult to understand for maintenance.

+10  A: 

Generics don't exist after bytecode has been compiled.

Restrictions from this: You can't implement / extend two or more interfaces / classes that would be same without the generic parameter and are different with the generic parameter.

What you could do if you really really want type safety is:

interface Foo<T extends Foo<?>> extends Comparable<T>
interface Bar<T extends Bar<?>> extends Comparable<T>
abstract class BarDescription<T extends Bar<?>> implements Bar<T>
class FooBar extends BarDescription<FooBar> implements Foo<FooBar>
mkorpela
This is sort of working. I'm still trying to wrap my head around it. Why do we need the <?> I wish I had made my original post with more concrete examples.
Also, isn't it redundant to have interface Foo<T extends Foo<?>> ? Another issue is that I have many classes which extend BarDescription but not Foo, and now all these have to define the type for BarDescription and implement compareTo(AnotherBarDescription), such as : class AnotherBarDescription extends BarDescription<AnotherBarDescription>
Yes it is redundant .. but Java Generics have there problems. One big problem is inheritance (super type is ? instead of Object). For example Foo<Object> foo = new Foo<SomeObject>() isn't allowed.
mkorpela
If you have objects that need to be compared with other objects that only implement Foo or Bar then I suggest that you use Jim Kileys answer and implement two Comparators (one for Foo and one for BarDescription).
mkorpela
+1  A: 

I'd write a couple of Comparators and be done with it.

Jim Kiley
+1  A: 

Having multiple implementations of generic interfaces would run into problems when you consider wildcards.

This does not depend upon erasure.

Tom Hawtin - tackline