Can someone explain why this compiles in JDK 1.6, but not in JDK 1.7 from which I get the error message:
java: Example is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable?
import java.util.concurrent.*;
public class Example implements ScheduledFuture
{
@Override public long getDelay(TimeUnit unit){ return 0; }
@Override public int compareTo(Delayed o) { return 0; }
@Override public boolean cancel(boolean mayInterruptIfRunning) { return false; }
@Override public boolean isCancelled() { return false; }
@Override public boolean isDone() { return false; }
@Override public Object get() throws InterruptedException, ExecutionException { return null; }
@Override public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return null; }
}
For your information, the methods in this class are generated by IntelliJ after writing just the class declaration.
The error message indicates that the compiler requires that the class declare a compareTo method that takes an Object
typed parameter and this class takes a Delayed
.
However, the ScheduledFuture
interface is defined as extending Delayed
which in turn extends Comparable<Delayed>
so to me everything seems to be in order.
If I just change the declaration to
private class Example implements ScheduledFuture<Object>
it compiles.
I am guessing it has to do with type erasure somehow but I can't really explain it to satisfy myself.