/* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. */ /** * The sample illustrates rules to resolve conflicts between inheritance * candidates with default methods. There are two simple rules: *
* new Tuna().swim(); //"Tuna swims under water" output is suspected here **/ public static class Tuna extends Fish implements Swimable { } /** * The interface that overrides {@link #swim} method (subtype of * {@link Swimable}) */ public interface Diveable extends Swimable { @Override default void swim() { System.out.println("I can swim on a water surface"); } default void dive() { System.out.println("I can dive"); } } /** * This class is used for illustration rule 2. See the source code of the * {@link #main} method *
* new Duck().swim(); //"I can swim on a water surface" output is suspected here **/ public static class Duck implements Swimable, Diveable { } public static void main(String[] args) { // Illustrates the rule 1. The Fish.swim() implementation wins //"Tuna swims under water" output is suspected here new Tuna().swim(); // Illustrates the rule 2. The Diveable.swim() implementation wins //"I can swim on a water surface" output is suspected here new Duck().swim(); } }