Theorie Duplicate default methods

CSHW89

Bekanntes Mitglied
Hallo zusammen,

ich habe mal eine kleine theoretische Frage. Durch "default" Methoden gibt es ja bekanntlich bei folgenden Code die Fehlermeldung "Duplicate default methods named func with the parameters () and () are inherited from the types Test.I2 and Test.I1":
Java:
public class Test {
    interface I1 {
        default public int func() {
            return 2;
        }
    }
    interface I2 {
        default public int func() {
            return 3;
        }
    }
    static class ClassA implements I1,I2 {
    }
}
Warum gibt es aber bei folgendem Code keine Fehlermeldung? Warum gewinnt dort die Methode von "Both"?
Java:
public class Test {
    interface I1 {
        default public int func() {
            return 2;
        }
    }
    interface I2 {
        default public int func() {
            return 3;
        }
    }
    interface Both extends I1,I2 {
        default public int func() {
            return 4;
        }
    }
    static class ClassB implements I1,Both {
    }
    public static void main(String[] args) {
        ClassB obj = new ClassB();
        System.out.println(obj.func());
    }
}
 
Aus JLS 8.4.8:

A class C inherits from its direct superclass type and direct superinterface types all abstract and default (§9.4) methods m for which all of the following are true:
[....]
  • There exists no method m' that is a member of the direct superclass type or a direct superinterface type of C, D' (m distinct from m', D distinct from D'), such that m' overrides from the class or interface of D' the declaration of the method m (§8.4.8.1, §9.4.1.1).
mit: D = I1, m=I1#func, D' = Both, m'= Both#func
 

Neue Themen


Zurück
Oben