I want to create a generic class that takes elements of some generic type that are comparable. So I do:
public class Foo<T extends Comparable<T>>
and inside the class Foo
I have things like:
public void bar(T t)
and I'm assured that I can write code like this: t.compareTo(v)
.
Question 1: Why when using generics we have extends
instead of implements
for an interface? Comparable
is not a class.
\\
Assume now that I want to create another similar class to the above also implementing
the bar
method. I thought of creating this interface:
public interface Face<T extends Comparable<T>> {
public void bar(T t);
}
and then I change class Foo
to implement Face
(public class Foo<T extends Comparable<T>> implements Face
).
Question 2: When doing this I get the following compile error:
The method bar(T) of type Foo must override or implement a supertype method.
Why is this?
When I tell Eclipse to add the unimplemented methods
I get:
public void bar(Comparable t)
instead of ... bar(T t)
.