DEV Community

loizenai
loizenai

Posted on

Java 9 Private Interface Method

https://grokonez.com/java/java-9/java-9-private-interface-method

Java 9 Private Interface Method

In this article, we're gonna take a look at Java 9 Private Interface Method which helps us avoid duplicate code and keep encapsulation for interface.

1. Java 8 - Interface

Java 8 provides 2 new features for Interface: default methods and static methods.

This is an example of Java 8 interface:


public interface ICustomerService {

    default void healthcare(String name) {
        System.out.println(name + " registers for customer service.");
        System.out.println("-- get HealthCare Service.");
    }

    default void consult(String name) {
        System.out.println(name + " registers for customer service.");
        System.out.println("-- get Consultation Service.");
    }
}

To make code easy to read and clear, we can create new register() method inside:

More at:

https://grokonez.com/java/java-9/java-9-private-interface-method

Java 9 Private Interface Method

Top comments (0)