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

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay