Java requires implementing default method

Viewed 97

I have Base interface:

public interface Doable {
    void doAction(String str);
}

I have extending interface:

public interface DoubleDoable extends Doable{
  @Override 
  default void doAction(String str) {
      doOnce();
      doOnce();
  }

  void doOnce();
}

And I have an implementation:

public class Action implements DoubleDoable {
    public void doOnce() {
      System.out.println(123);
    }
}

However, it is not compiled, as: Error:(10, 8) java: Action is not abstract and does not override abstract method doAction(java.lang.String) in Doable

Am I doing something wrong?

2 Answers

If you're using a Java 8 compiler, the only way that your code can cause that compilation error is if the value of the -source flag that is passed to the compiler is set to 1.7 or lower.

Something like:

javac -source 1.7 ...

If you're using maven, the property setting below will have the same effect.

<maven.compiler.source>1.7</maven.compiler.source>

You can't call method do because it's java reserved word. All list of Java keywords you can find here.

Related