DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Execution Type Models in Node.js
  • Why You Should Consider Using React Router V6: An Overview of Changes
  • JavaFX Goes Mobile
  1. DZone
  2. Coding
  3. Java
  4. More Power to Interface in Java 9

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.

Arun Pandey user avatar by
Arun Pandey
·
Apr. 07, 17 · Tutorial
Like (35)
Save
Tweet
Share
56.67K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Interface (computing) Java (programming language)

Opinions expressed by DZone contributors are their own.

Trending

  • Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Execution Type Models in Node.js
  • Why You Should Consider Using React Router V6: An Overview of Changes
  • JavaFX Goes Mobile

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: