HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 9 / Private Methods in Interface – Java 9

Private Methods in Interface – Java 9

Java 9 onward, you are allowed to include private methods in interfaces. Using private methods, now encapsulation is possible in interfaces as well.

In this java 9 tutorial, we will learn about interface private methods in detail.

Table of Contents

Interfaces till Java 7
Static and defaults methods since Java 8
Private methods since java 9
Java 9 Private Interface Method Example
Summary

Interfaces till Java 7

In Java 7 and all earlier versions, interfaces were very simple. They could only contain public abstract methods. These interface methods MUST be implemented by classes which choose to implement the interface.

public interface CustomInterface {
    public abstract void method();
}

public class CustomClass implements CustomInterface {
    @Override
    public void method() {
        System.out.println("Hello World");
    }

    public static void main(String[] args){
        CustomInterface instance = new CustomClass();
        instance.method();
    }
}

Output:

Hello World

Static and defaults methods since Java 8

From Java 8, apart from public abstract methods, you can have public static methods and public default methods.

public interface CustomInterface {
    
    public abstract void method1();
    
    public default void method2() {
        System.out.println("default method");
    }
    
    public static void method3() {
        System.out.println("static method");
    }
}

public class CustomClass implements CustomInterface {

    @Override
    public void method1() {
        System.out.println("abstract method");
    }
    
    public static void main(String[] args){
        CustomInterface instance = new CustomClass();
        instance.method1();
        instance.method2();
        CustomInterface.method3();
    }
}

Output:

abstract method
default method
static method
Access modifier ‘public’ is optional in all above interface method declarations. I have added them to improve readability only.

Private methods since java 9

Since java 9, you will be able to add private methods and private static method in interfaces.

These private methods will improve code re-usability inside interfaces. Foe example, if two default methods needed to share code, a private interface method would allow them to do so, but without exposing that private method to it’s implementing classes.

Using private methods in interfaces have four rules :

  1. Private interface method cannot be abstract.
  2. Private method can be used only inside interface.
  3. Private static method can be used inside other static and non-static interface methods.
  4. Private non-static methods cannot be used inside private static methods.
public interface CustomInterface {
    
    public abstract void method1();
    
    public default void method2() {
        method4();  //private method inside default method
        method5();  //static method inside other non-static method
        System.out.println("default method");
    }
    
    public static void method3() {
        method5(); //static method inside other static method
        System.out.println("static method");
    }
    
    private void method4(){
        System.out.println("private method");
    } 
    
    private static void method5(){
        System.out.println("private static method");
    } 
}

public class CustomClass implements CustomInterface {

    @Override
    public void method1() {
        System.out.println("abstract method");
    }
    
    public static void main(String[] args){
        CustomInterface instance = new CustomClass();
        instance.method1();
        instance.method2();
        CustomInterface.method3();
    }
}

Output:

abstract method
private method
private static method
default method
private static method
static method

Java 9 Private Interface Method Example

Let’s see a demo to understand the private interface method’s usage.

I am creating a calculator class with two functions. First function will accept some integers and add all even numbers in it. Second function will accept some integers and add all odd numbers in it.

CustomCalculator.java – Interface

import java.util.function.IntPredicate;
import java.util.stream.IntStream;

public interface CustomCalculator 
{
    default int addEvenNumbers(int... nums) {
        return add(n -> n % 2 == 0, nums);
    }

    default int addOddNumbers(int... nums) {
        return add(n -> n % 2 != 0, nums);
    }

    private int add(IntPredicate predicate, int... nums) { 
        return IntStream.of(nums)
                .filter(predicate)
                .sum();
    }
}

Main.java – Class

public class Main implements CustomCalculator {

    public static void main(String[] args) {
        CustomCalculator demo = new Main();
        
        int sumOfEvens = demo.addEvenNumbers(1,2,3,4,5,6,7,8,9);
        System.out.println(sumOfEvens);
        
        int sumOfOdds = demo.addOddNumbers(1,2,3,4,5,6,7,8,9);
        System.out.println(sumOfOdds);
    } 
}

Output:

20
25

Summary

In short, java 9 private interface methods can be static or instance. In both cases, the private method is not inherited by sub-interfaces or implementations. They are mainly there to improve code re-usability within interface only – thus improving encapsulation.

Let’s revisit all the allowed method types in java 9.

Method TypeSince When
public abstractJava 7
public defaultJava 8
public staticJava 8
privateJava 9
private staticJava 9

Drop me your questions in comments sections.

Happy Learning !!

Ref: JEP 213

Download Sourcecode

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. rakesh

    January 1, 2020

    hello, everyone I have a doubt in java 9 features in interface is that if private static method can also used in default method then what is the purpose of private instance method

  2. Saurabh

    March 26, 2019

    Nice info with examples

  3. priya

    March 30, 2018

    A Java interface is used to specify what is to be done but not how it is to be done. Inheritance is generally in the context of interfaces when extending an interface is necessary.

  4. Saurabh

    August 1, 2017

    Point no 4 which says non static method can not be used inside static method. This is logically obvious also as at that time non static methods are not available in memory. Please highlight this logic and put it as a general rule.

Comments are closed on this article!

Search Tutorials

Java 9 Tutorial

  • Java 9 – Introduction
  • Java 9 – Compact Strings
  • Java 9 – Modules
  • Java 9 – JShell
  • Java 9 – Stream of Dates
  • Java 9 – Stream API Improvements
  • Java 9 – Immutable Collections
  • Java 9 – Interface Private Methods

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces