Implementing two interfaces with two default methods of the same signature in Java 8

前端 未结 6 566
悲哀的现实
悲哀的现实 2020-11-30 06:10

Suppose I have two interfaces:

public interface I1
{
    default String getGreeting() {
        return \"Good Morning!\";
    }
}

public interface I2
{
             


        
6条回答
  •  囚心锁ツ
    2020-11-30 06:47

    I believe the rule is that the class implementing the duplicate default methods 'must' override the implementation.. The following compiles and runs fine...

    public class DupeDefaultInterfaceMethods {
    
    interface FirstAbility {
        public default boolean doSomething() {
            return true;
        }
    }
    
    interface SecondAbility {
        public default boolean doSomething() {
            return true;
        }
    }
    
    class Dupe implements FirstAbility, SecondAbility {
        @Override
        public boolean doSomething() {
            return false;
        }
    }
    
    public static void main(String[] args) {
        DupeDefaultInterfaceMethods ddif = new DupeDefaultInterfaceMethods();
        Dupe dupe = ddif.new Dupe();
        System.out.println(dupe.doSomething());
    
        }
    }
    

    > false
    

提交回复
热议问题