Friday, 3 February 2017

Inheritance : multiple default methods




/** *Default methods and abstract methods in interfaces are inherited like instance methods. However, when the supertypes of a class or interface provide multiple default methods with the same signature, the Java compiler follows inheritance rules to resolve the name conflict. These rules are driven by the following two principles:
 Instance methods are preferred over interface default methods. * */

Pegasus.java



 class Horse {
    public String identifyMyself() {
        return "I am a horse.";
    }
}
 interface Flyer {
    default public String identifyMyself() {
        return "I am able to fly.";
    }
}
 interface Mythical {
    default public String identifyMyself() {
        return "I am a mythical creature.";
    }
}public class Pegasus extends Horse implements Flyer, Mythical {
    public static void main(String... args) {
        Pegasus myApp = new Pegasus();
        System.out.println(myApp.identifyMyself());
    }
}

No comments:

Post a Comment