Avoiding Redundancy in Interface Code - Java Object Oriented Design

Java examples for Object Oriented Design:interface

Introduction

Java 9 can include private methods within an interface.

A private method is only available within that interface, and it cannot be used by any class that implements the interface.

Each default method implementation can make use of the private method.

Demo Code

interface MyInter{
    /*from w  ww.j av  a 2 s  . c  o m*/
    public default double a(double length, double width, double depth){
        return myMethod(length, width, depth);
    }
 
    public default double b(double length, double width,double shallowDepth, double middleDepth, double deepDepth){
        return myMethod(length, width, 6);
    }
    
    /**
     * Standard square or rectangular volume calculation. 
     */
    static double myMethod(double length, double width, double depth){
        return length * width * depth * 7.5;
    }
}

Related Tutorials