1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 */ 5 6 7 /** 8 * The code sample diamond inheritance with <b>default methods</b>. If there's 9 * not already a unique method implementation to inherit, you have to provide 10 * it. The inheritance diagram like below: 11 * <pre> 12 * Animal 13 * / \ 14 * Horse Bird 15 * \ / 16 * Pegasus 17 * </pre> 18 * 19 * Both {@link Horse} and {@link Bird} interfaces implements <code>go</code> 20 * method. The {@link Pegasus} class have to override <code>go</code> method. 21 * 22 * The new syntax of super-call is used here: 23 * <pre> 24 * <interface_name>.super.<method>(...); 25 * e.g.: Horse.super.go(); 26 * </pre> So, Pegasus goes like a Horse 27 */ 28 public class DiamondInheritance { 29 30 /** 31 * Base interface to illustrate diamond inheritance . 32 * 33 * @see DiamondInheritance 34 */ 35 public interface Animal { 36 37 void go(); 38 } 39 40 /** 41 * Interface to illustrate diamond inheritance. 42 * 43 * @see DiamondInheritance 44 */ 45 public interface Horse extends Animal { 46 47 @Override 48 default void go() { 49 System.out.println(this.getClass().getSimpleName() + " walks on four legs"); 50 } 51 } 52 53 /** 54 * Interface to illustrate diamond inheritance. 55 * 56 * @see DiamondInheritance 57 */ 58 public interface Bird extends Animal { 59 60 @Override 61 default void go() { 62 System.out.println(this.getClass().getSimpleName() + " walks on two legs"); 63 } 64 } 65 66 /** 67 * Class to illustrate diamond inheritance. 68 * Pegasus have to mix Horse and Bird behavior. 69 * 70 * @see DiamondInheritance 71 */ 72 public static class Pegasus implements Horse, Bird { 73 74 @Override 75 public void go() { 76 Horse.super.go(); 77 } 78 } 79 80 public static void main(String[] args) { 81 new Pegasus().go(); 82 } 83 }