Call default interface method which is overridden in superclass [duplicate]
Asked Answered
P

1

5

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.

Polyzoarium answered 11/9, 2015 at 6:5 Comment(1)
C
7

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

@Override
public void toDO() {
    Inter2.super.toDO();
}
Chalk answered 11/9, 2015 at 6:7 Comment(1)
Java is quite logical here - for you can implement multiple interfaces, that is why you have to prefix super. If super is alone, then it refers to the only one superclass that can be extended.Maddox

© 2022 - 2024 — McMap. All rights reserved.