Java 9 Private Methods in Interface with Examples

As we know that Java 8 allows us to include default and static methods in an interface. For example:
public interface CustomInterface {
     
    public abstract void method1();
     
    public default void method2() {
        System.out.println("default method");
    }
     
    public static void method3() {
        System.out.println("static method");
    }
}
Learn Java 8 at https://www.javaguides.net/p/java-8.html
Java 9 onward, we can able to include private methods in interfaces. Using private methods, now encapsulation is possible in interfaces as well.

Video

Java 9 - Private Methods in Interface

Since java 9, we can able to add private methods and private static methods in interfaces.
These private methods will improve code re-usability inside interfaces. For example, if two default methods needed to share code, a private interface method would allow them to do so, but without exposing that private method to it’s implementing classes.
Using private methods in interfaces have four rules :
  • The private interface method cannot be abstract.
  • A private method can be used only inside interface.
  • A private static method can be used inside other static and non-static interface methods.
  • A private non-static method cannot be used inside private static methods.
package net.javaguides.corejava.java9;

/**
 * Java 9 - private methods and private static method in interfaces
 * @author Ramesh fadatare
 *
 */
public class PrivateInterfaceMethod implements CustomInterface {

    @Override
    public void abstractMethod() {
        System.out.println("abstract Method implementation");
    }

    public static void main(String[] args) {
        CustomInterface customInterface = new PrivateInterfaceMethod();
        customInterface.defaultMethod();
        customInterface.abstractMethod();
        CustomInterface.staticMethod();
    }
}

interface CustomInterface {

    public abstract void abstractMethod();

    public
    default void defaultMethod() {
        privateMethod(); //private method inside default method
        privateStaticMethod(); //static method inside other non-static method
        System.out.println("default method");
    }

    public static void staticMethod() {
        privateStaticMethod(); //static method inside other static method
        System.out.println("static method");
    }

    private void privateMethod() {
        System.out.println("private method");
    }

    private static void privateStaticMethod() {
        System.out.println("private static method");
    }
}

Java 9 Private Interface Method Example

CustomCalculator.java – Interface

import java.util.function.IntPredicate;
import java.util.stream.IntStream;
 
public interface CustomCalculator 
{
    default int addEvenNumbers(int... nums) {
        return add(n -> n % 2 == 0, nums);
    }
 
    default int addOddNumbers(int... nums) {
        return add(n -> n % 2 != 0, nums);
    }
 
    private int add(IntPredicate predicate, int... nums) { 
        return IntStream.of(nums)
                .filter(predicate)
                .sum();
    }
}

Main.java – Class

public class Main implements CustomCalculator {
 
    public static void main(String[] args) {
        CustomCalculator demo = new Main();
         
        int sumOfEvens = demo.addEvenNumbers(1,2,3,4,5,6,7,8,9);
        System.out.println(sumOfEvens);
         
        int sumOfOdds = demo.addOddNumbers(1,2,3,4,5,6,7,8,9);
        System.out.println(sumOfOdds);
    } 
}
Output:
20
25

Related Java 9 Posts


Comments