alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (DiamondInheritance.java)

This example Java source code file (DiamondInheritance.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

animal, bird, diamondinheritance, horse, override, pegasus, string

The DiamondInheritance.java Java example source code

/*
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * This sample diamond interface inheritance with <b>default methods.
 * If there's not already a unique method implementation to inherit,
 * you must provide it. The inheritance diagram is similar to the following:
 * <pre>
 *                   Animal
 *                    /   \
 *                 Horse   Bird
 *                    \   /
 *                   Pegasus
 * </pre>
 *
 * Both {@link Horse} and {@link Bird} interfaces implements the <code>go
 * method. The {@link Pegasus} class have to overrides the
 * <code>go method.
 *
 * The new syntax of super-call is used here:
 * <pre>
 *     <interface_name>.super.<method>(...);
 *     For example:  Horse.super.go();
 * </pre> So, Pegasus moves like a horse.
 */
public class DiamondInheritance {

    /**
     * Base interface to illustrate the diamond inheritance.
     *
     * @see DiamondInheritance
     */
    public interface Animal {

        /**
         * Return string representation of the "go" action for concrete animal
         *
         * @return string representation of the "go" action for concrete animal
         */
        String go();
    }

    /**
     * Interface to illustrate the diamond inheritance.
     *
     * @see DiamondInheritance
     */
    public interface Horse extends Animal {

        /**
         * Return string representation of the "go" action for horse
         *
         * @return string representation of the "go" action for horse
         */
        @Override
        default String go() {
            return this.getClass().getSimpleName() + " walks on four legs";
        }
    }

    /**
     * Interface to illustrate the diamond inheritance.
     *
     * @see DiamondInheritance
     */
    public interface Bird extends Animal {

        /**
         * Return string representation of the "go" action for bird
         *
         * @return string representation of the "go" action for bird
         */
        @Override
        default String go() {
            return this.getClass().getSimpleName() + " walks on two legs";
        }

        /**
         * Return string representation of the "fly" action for bird
         *
         * @return string representation of the "fly" action for bird
         */
        default String fly() {
            return "I can fly";
        }
    }

    /**
     * Class to illustrate the diamond inheritance. Pegasus must mix horse and
     * bird behavior.
     *
     * @see DiamondInheritance
     */
    public static class Pegasus implements Horse, Bird {

        /**
         * Return string representation of the "go" action for the fictitious
         * creature Pegasus
         *
         * @return string representation of the "go" action for the fictitious
         * creature Pegasus
         */
        @Override
        public String go() {
            return Horse.super.go();
        }
    }

    /**
     * Illustrate the behavior of the {@link Pegasus} class
     *
     * @param args command line arguments
     */
    public static void main(final String[] args) {
        System.out.println(new Pegasus().go());
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java DiamondInheritance.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.