Search code examples
javainterfacejava-8default-method

Call default interface method which is overridden in superclass


I have a interface and abstract class.

public class TEST extends Abstract implements Inter2{
    void print() {
        toDO();
    }

    public static void main(String[] args) {
        new TEST().toDO();
    }
}

abstract class Abstract {

    public void toDO() {
        System.out.println("Abstract is called");
    }
}

interface Inter2 {

    default void toDO() {
        System.out.println("Inter2 is called");
    }
}

I want to force class interface default methods instead of abstract class.


Solution

  • You'll have to override toDO in the TEST class:

    @Override
    public void toDO() {
        Inter2.super.toDO();
    }