0

I have a class that implements an interface, which in turn extends another interface. My code looks like this:

interface InterfaceOne {
    default void log() {};
}

interface InterfaceTwo extends InterfaceOne {
    void log();
}

public class TestClass implements InterfaceTwo {
    @Override
    public void log() {}

    public static void main(String[] args) {}
}

Why should I implement in TestClass method log()? Isn't it correct that TestClass does inherit default void log() method which is defined in InterfaceOne? Because TestClass implements InterfaceTwo which extends InterfaceOne.

Why should I implement the log() method if is already defined in InterfaceOne?

shmosel
  • 45,768
  • 6
  • 62
  • 130
Joan P.
  • 1,891
  • 3
  • 20
  • 42
  • 1
    Why don't you remove the method from `InterfaceTwo`? – shmosel Oct 31 '17 at 18:23
  • InterfaceTwo tells to override `void log()` as it declares the method. The method declaration of InterfaceOne is omitted then. – Cedric Oct 31 '17 at 18:23
  • I can remote it as you say, but I still not understand this behavior. Why is this happening? – Joan P. Oct 31 '17 at 18:24
  • The constraint on the default method is that it can be implemented only in the terms of calls to other interface methods, with no reference to a particular implementation's state. –  Oct 31 '17 at 18:24
  • 1
    `InterfaceTwo.log()` overrides the `default` in `IntefaceOne.log()` making it unimplemented again. –  Oct 31 '17 at 18:25
  • The referenced question doesn't really answer this question! – Cedric Oct 31 '17 at 18:26

0 Answers0