Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/sample/lambda/DefaultMethods/Inheritance.java
38829 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/**32* The sample illustrates rules to resolve conflicts between inheritance33* candidates with <b>default methods</b>. There are two simple rules:34* <ul>35* <li>Class wins. If the superclass has a concrete or abstract declaration of36* this method, then it is preferred over all defaults.</li>37* <li>Subtype wins. If an interface extends another interface, and both provide38* a default, then the more specific interface wins. </li>39* </ul>40*/41public class Inheritance {4243/**44* The behavior of an creature that can swim45*/46public interface Swimable {4748/**49* Return string representation of the swim action for a creature that50* can swim51*52* @return string representation of the swim action for a creature53* that can swim54*/55default String swim() {56return "I can swim.";57}58}5960/**61* The abstract class that overrides {@link #swim} method62*/63public abstract static class Fish implements Swimable {6465/**66* Return string representation of the swim action for a fish67*68* @return string representation of the swim action for a fish69*/70@Override71public String swim() {72return this.getClass().getSimpleName() + " swims under water";73}74}7576/**77* This class is used for the illustration rule of 1. See the source code78* of the {@link #main} method.79* <pre>80* System.out.println(new Tuna().swim()); //"Tuna swims under water" output is suspected here81* </pre>82*/83public static class Tuna extends Fish implements Swimable {84}8586/**87* The behavior of an creature that can dive: the interface that overrides88* {@link #swim} method (subtype of {@link Swimable})89*/90public interface Diveable extends Swimable {9192/**93* Return string representation of the swim action for a creature that94* can dive95*96* @return string representation of the swim action for a creature97* that can dive98*/99@Override100default String swim() {101return "I can swim on the surface of the water.";102}103104/**105* Return string representation of the dive action for a creature that106* can dive107*108* @return string representation of the dive action for a creature109* that can dive110*/111default String dive() {112return "I can dive.";113}114}115116/**117* This class is used for the illustration of rule 2. See the source code118* of the {@link #main} method119* <pre>120* //"I can swim on the surface of the water." output is suspected here121* System.out.println(new Duck().swim());122* </pre>123*/124public static class Duck implements Swimable, Diveable {125}126127/**128* Illustrate behavior of the classes: {@link Tuna} and {@link Duck}129*130* @param args command line arguments131*/132public static void main(final String[] args) {133// Illustrates rule 1. The Fish.swim() implementation wins134//"Tuna swims under water" is output135System.out.println(new Tuna().swim());136137// Illustrates rule 2. The Diveable.swim() implementation wins138//"I can swim on the surface of the water." is output139System.out.println(new Duck().swim());140}141}142143144