Java 9 Private Methods in Interface

Posted By : Rahul Sarkar | 30-Apr-2018

Starting from Java 9, you can add private methods and private static methods to the interface.

 

These private methods will improve the reuse of code within the interface. For example, if two default methods require shared code, the private interface method will allow them to do so, but will not expose the private method to the class's implementation.

 

There are four rules for using private methods in an interface:

Dedicated interface methods cannot be abstract.
Private methods can only be used in interfaces.
Static private methods can be used in other static and non-static interface methods.
Non-static private methods cannot be used in private static methods.

 

public interface CustomInterface {
     
    public abstract void method1();
     
    public default void method2() {
        method4();  //private method inside default method
        method5();  //static method inside other non-static method
        System.out.println("default method");
    }
     
    public static void method3() {
        method5(); //static method inside other static method
        System.out.println("static method");
    }
     
    private void method4(){
        System.out.println("private method");
    }
     
    private static void method5(){
        System.out.println("private static method");
    }
}
 
public class CustomClass implements CustomInterface {
 
    @Override
    public void method1() {
        System.out.println("abstract method");
    }
     
    public static void main(String[] args){
        CustomInterface instance = new CustomClass();
        instance.method1();
        instance.method2();
        CustomInterface.method3();
    }
}
 
Output:
 
abstract method
private method
private static method
default method
private static method
static method
           

 

 

Private Interface Method Example

Let's take a look at a demo to understand the use of private interface methods.

I am creating a calculator class with two functions. The first function will accept some integers and add all the even numbers. The second function will accept some integers and add all odd numbers.

 

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();
    }
}

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

Thanks.

About Author

Author Image
Rahul Sarkar

Rahul is an intellectual web app developer, he has good knowledge of C, Java, Java Script, Jquery. Apart from this he likes to travel and explore new places.

Request for Proposal

Name is required

Comment is required

Sending message..