Here is a simplified example showing my problem:
import java.util.List;
public interface SingleTask extends List<Runnable>, Runnable {
default Runnable get(final int x) {
if (x != 0) {
throw new IndexOutOfBoundsException();
}
return this;
}
default int size() {
return 1;
}
}
import java.util.AbstractList;
public class MyTask extends AbstractList<Runnable> implements SingleTask {
@Override
public void run() {
System.out.println("hello");
}
}
In SingleTask I provide implementations for the methods get and size, which are the only abstract methods from AbstractList. However, when I compile MyTask, I still get errors like:
The type MyTask must implement the inherited abstract method AbstractCollection.size()
or
MyTask.java:3: error: MyTask is not abstract and does not override abstract method get(int) in AbstractList
(depending on the compiler). I am, of course, using java 8.
So I have two questions:
- Why am I getting these errors? I was expecting it to recognize the default implementations.
- If it's not supposed to work like that, then what's the simplest way to use those two methods in
MyTaskwithout copying the whole code?
List<Runnable>(what does that mean semantically anyway?), instead create an interfaceTaskwith apublic List<Runnable> getRunnables();method. Failing that, simply makeSingleTaskan abstract class instead of an interface.size()not implemented: this is probably an Eclipse bug.javac1.8.0_51 is choking onget(int)not being implemented and it's right: it is not implemented.sizeandgetare equally implemented/not implementedSingleTask is a singleton implementationThere, you've just said it. If it's an implementation, it should be a class, not an interface. What you're trying to use default methods here for looks a bit like traits, which they aren't designed for.Listimplementation where even crucial methods likeequalsorhashCodeare broken as they might end up with aStackOverflowError. What’s the point of this recursive data structure?