Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

More Power to Interface in Java 9

DZone's Guide to

More Power to Interface in Java 9

Among the many improvements coming to Java 9 is the ability to house private methods in your Interfaces. Let's take a look at why and how it's done.

· Java Zone
Free Resource

Managing a MongoDB deployment? Take a load off and live migrate to MongoDB Atlas, the official automated service, with little to no downtime.

Interface is a very popular way to expose our APIs. Until Java 7 it was getting used as a contract (abstract method) where a child class was obliged to implement the contract; but after Java 8, Interface got more power — to where we can have static and default methods.

This guide will walk you through some Java 8 Interface enhancements.

In Java 9, Interface is getting more power, to the point where we can define private methods as well. Let us understand why we need private methods in Interfaces.

Suppose we are defining a ReportGenerator Interface in Java8 as below:

package com.test.interfacejava8;

/**
 * Created by arun.pandey.
 */
public interface ReportGeneratorJava8 {

    /**
     * Need to get implemented as per ReportGenerator class
     * @param reportData
     * @param schema
     */
    void generateReport(String reportData, String schema);

    /**
     * Get the ready data
     * @param reportSource
     * @return
     * @throws Exception
     */
    default String getReportData(String reportSource) throws Exception {

        String reportData = null;

        if (null == reportSource) {
            throw new Exception("reportSource can't be null....");
        }

        if (reportSource.equalsIgnoreCase("DB")) {
            System.out.println("Reading the data from DB ....");
            //logic to get the data from DB
            reportData = "data from DB";
        } else if (reportSource.equalsIgnoreCase("File")) {
            System.out.println("Reading the data from FileSystem ....");
            //logic to get the data from File
            reportData = "data from File";
        } else if (reportSource.equalsIgnoreCase("Cache")) {
            System.out.println("Reading the data from Cache ....");
            //logic to get the data from Cache
            reportData = "data from Cache";
        }

        System.out.println("Formatting the data to create a common standard");

        /** Format the data and then return **/
        //logic to format the data
        return reportData;
    }
}


And the implementation class could be HtmlReportGeneratorJava8:

package com.test.interfacejava8;

/**
 * Created by arun.pandey.
 */
public class HtmlReportGeneratorJava8 implements ReportGeneratorJava8 {
    @Override
    public void generateReport(String reportData, String schema) {
        //HTML Specific Implementation according to given schema
    }
}


Method 'getReportData' looks pretty messy, as there is a lot of logic that could be kept in a separate method and that can be called in 'getReportData'. To achieve that, we need a private method, as we don't want to expose these methods to outside the world.

Another thing, ReportGeneratorJava8 interface is formatting the data after getting it from the source. So we can have a common method named 'formatData' defined as private in the interface. So the interface could be rewritten as below:

package com.test.interfacejava9;

/**
 * Created by arun.pandey.
 */
public interface ReportGeneratorJava9 {

    /**
     * Need to get implemented as per ReportGenerator class
     * @param reportData
     * @param schema
     */
    void generateReport(String reportData, String schema);

    /**
     * Reading the report data from DB
     * @return
     */
    private String getReportDataFromDB() {
        System.out.println("Reading the data from DB ....");

        //logic to get the data from DB
        String reportData = "data from DB";
        return formatData(reportData);
    }

    /**
     * Reading the report data from FileSystem
     * @return
     */
    private String getReportDataFromFile() {
        System.out.println("Reading the data from FileSystem ....");

        //logic to get the data from File
        String reportData = "data from File";
        return formatData(reportData);
    }

    /**
     * Reading the report data from cache
     * @return
     */
    private String getReportDataFromCache() {
        System.out.println("Reading the data from Cache ....");

        //logic to get the data from Cache
        String reportData = "data from Cache";
        return formatData(reportData);
    }

    /**
     * Formatting the data to create a common standardized data,
     * as it's coming from different systems
     * @param reportData
     * @return
     */
    private String formatData(String reportData) {
        System.out.println("Formatting the data to create a common standard");

        /** Format the data and then return **/
        //logic to format the data
        return reportData;
    }

    /**
     * Get the ready data
     * @param reportSource
     * @return
     * @throws Exception
     */
    default String getReportData(String reportSource) throws Exception {

        String reportData = null;

        if (null == reportSource) {
            throw new Exception("reportSource can't be null....");
        }

        if (reportSource.equalsIgnoreCase("DB")) {
            reportData = getReportDataFromDB();
        } else if (reportSource.equalsIgnoreCase("File")) {
            reportData = getReportDataFromFile();
        } else if (reportSource.equalsIgnoreCase("Cache")) {
            reportData = getReportDataFromCache();
        }
        return reportData;
    }
}


Now the above implementation looks pretty clean, and we've seen the need of private methods in Interface.

Summary of Interface Enhancements

  • Constants (until Java 1.7)

  • Method signatures (until Java 1.7)

  • Nested types (until Java 1.7)

  • Default methods (since 1.8)

  • Static methods (since 1.8)

  • Private methods (since 1.9)

  • Private static methods (since 1.9)

Enjoy the power of Java 9's Interface.

MongoDB Atlas is the easiest way to run the fastest-growing database for modern applications — no installation, setup, or configuration required. Easily live migrate an existing workload or start with 512MB of storage for free.

Topics:
interface ,java ,java 9 ,tutorial ,private methods

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}