Logo

dev-resources.site

for different kinds of informations.

Java 9 Private Interface Method

Published at
4/28/2021
Categories
java9
java
interface
Author
loizenai
Categories
3 categories in total
java9
open
java
open
interface
open
Author
8 person written this
loizenai
open
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

Featured ones: