Private Methods in Interface – Java 9

Lokesh Gupta

Java 9 onward, you are allowed to include private methods in interfaces. Using private methods, now encapsulation is possible in interfaces as well.

In this java 9 tutorial, we will learn about interface private methods in detail.

Table of Contents

Interfaces till Java 7
Static and defaults methods since Java 8
Private methods since java 9
Java 9 Private Interface Method Example
Summary

Interfaces till Java 7

In Java 7 and all earlier versions, interfaces were very simple. They could only contain public abstract methods. These interface methods MUST be implemented by classes which choose to implement the interface.

public interface CustomInterface {
    public abstract void method();
}

public class CustomClass implements CustomInterface {
    @Override
    public void method() {
        System.out.println("Hello World");
    }

    public static void main(String[] args){
        CustomInterface instance = new CustomClass();
        instance.method();
    }
}

Output:

Hello World

Static and defaults methods since Java 8

From Java 8, apart from public abstract methods, you can have public static methods and public default methods.

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

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
default method
static method
Access modifier ‘public’ is optional in all above interface method declarations. I have added them to improve readability only.

Private methods since java 9

Since java 9, you will be able to add private methods and private static method in interfaces.

These private methods will improve code re-usability inside interfaces. Foe 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 :

  1. Private interface method cannot be abstract.
  2. Private method can be used only inside interface.
  3. Private static method can be used inside other static and non-static interface methods.
  4. Private non-static methods cannot be used inside 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

Java 9 Private Interface Method Example

Let’s see a demo to understand the private interface method’s usage.

I am creating a calculator class with two functions. First function will accept some integers and add all even numbers in it. Second function will accept some integers and add all odd numbers in it.

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

Summary

In short, java 9 private interface methods can be static or instance. In both cases, the private method is not inherited by sub-interfaces or implementations. They are mainly there to improve code re-usability within interface only – thus improving encapsulation.

Let’s revisit all the allowed method types in java 9.

Method TypeSince When
public abstractJava 7
public defaultJava 8
public staticJava 8
privateJava 9
private staticJava 9

Drop me your questions in comments sections.

Happy Learning !!

Ref: JEP 213

Comments

Subscribe
Notify of
guest
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode