Private Methods in Java 9 Interfaces

Last Updated : 15 Sep, 2026

Java 9 introduced support for private methods inside interfaces. These methods allow common implementation logic to be shared by default and static methods without exposing that logic as part of the interface's public API.

  • Private interface methods are accessible only within the interface where they are declared.
  • They support code reuse between default and static methods of the same interface.
  • A private interface method may be either an instance method or a static method.

Interfaces Before Java 8

Before Java 8, interfaces primarily contained constant variables and abstract methods. An abstract method declared in an interface had to be implemented by a class that implemented the interface.

Java
interface TempI {

    void method(int n);
}

class Temp implements TempI {

    @Override
    public void method(int n) {

        if (n % 2 == 0)
            System.out.println("geeksforgeeks");
        else
            System.out.println("GEEKSFORGEEKS");
    }

    public static void main(String[] args) {

        TempI in1 = new Temp();
        TempI in2 = new Temp();

        in1.method(4);
        in2.method(3);
    }
}

Output
geeksforgeeks
GEEKSFORGEEKS

Here, method() is an abstract method, so Temp provides its implementation.

Java 8 Interface Changes

Java 8 introduced default methods and static methods in interfaces. This allowed interfaces to contain implementation code in addition to abstract methods and constants. An interface in Java 8 supports the following members:

  • Constant variables
  • Abstract methods
  • Default methods
  • Static methods
Java
interface TempI {

    void div(int a, int b);

    default void add(int a, int b)
    {

        System.out.print("Answer by Default method = ");
        System.out.println(a + b);
    }

    static void mul(int a, int b)
    {

        System.out.print("Answer by Static method = ");
        System.out.println(a * b);
    }
}

class Temp implements TempI {

    @Override public void div(int a, int b)
    {

        System.out.print("Answer by Abstract method = ");
        System.out.println(a / b);
    }

    public static void main(String[] args)
    {

        TempI in = new Temp();

        in.div(8, 2);
        in.add(3, 1);
        TempI.mul(4, 1);
    }
}

Output
Answer by Abstract method = 4
Answer by Default method = 4
Answer by Static method = 4

Here, div() is abstract, add() is a default method, and mul() is a static method.

Private Methods in Java 9 Interfaces

Java 9 introduced private methods and private static methods in interfaces. These methods are intended for internal code reuse within the same interface.

An interface supports the following members relevant to this topic:

  1. Constant variables
  2. Abstract methods
  3. Default methods
  4. Static methods
  5. Private methods
  6. Private static methods

Private methods are not inherited by implementing classes or subinterfaces. They are accessible only from within the interface where they are declared.

Java
interface TempI {

    void mul(int a, int b);

    default void add(int a, int b) {

        sub(a, b);
        div(a, b);

        System.out.print("Answer by Default method = ");
        System.out.println(a + b);
    }

    static void mod(int a, int b) {

        div(a, b);

        System.out.print("Answer by Static method = ");
        System.out.println(a % b);
    }

    private void sub(int a, int b) {

        System.out.print("Answer by Private method = ");
        System.out.println(a - b);
    }

    private static void div(int a, int b) {

        System.out.print("Answer by Private static method = ");
        System.out.println(a / b);
    }
}

class Temp implements TempI {

    @Override
    public void mul(int a, int b) {

        System.out.print("Answer by Abstract method = ");
        System.out.println(a * b);
    }

    public static void main(String[] args) {

        TempI in = new Temp();

        in.mul(2, 3);
        in.add(6, 2);
        TempI.mod(5, 3);
    }
}

Output
Answer by Abstract method = 6
Answer by Private method = 4
Answer by Private static method = 3
Answer by Default method = 8
Answer by Private static method = 1
Answer by Static method = 2

Here, sub() is a private instance method used by the default method add(). The private static method div() is used by both the default method and the static method mod().

Rules for Private Methods in Interfaces

The following rules apply to private methods in interfaces:

  • A private interface method must have a method body. It cannot be abstract.
  • A private method is accessible only within the interface where it is declared.
  • A private instance method is accessible from other instance methods of the same interface.
  • A private static method is accessible from static methods of the same interface.
  • A private instance method cannot be called directly from a static method because it requires an interface instance.
  • Private methods are not inherited by implementing classes or subinterfaces.
  • A private method must use the private modifier.

Advantages of Private Interface Methods

  • Code Reusability: Private methods allow common implementation logic to be reused by multiple default or static methods within the same interface.
  • Better Encapsulation: Implementation details remain hidden inside the interface and are not exposed to implementing classes or external code.
  • Cleaner Code: Repeated logic is moved into separate private methods, keeping default and static methods shorter and easier to understand.
  • Easy Maintenance: Common logic is maintained in one place, so changes to that logic do not require modifying multiple methods.
  • Controlled Access: Private methods are available only within the declaring interface, preventing unintended access from implementing classes or subinterfaces.

Use Cases of Private Interface Methods

Private interface methods are useful when multiple default or static methods share common internal logic.

  • Data Validation: A default method that processes user data and another default method that updates the data may use the same private validation method.
  • Logging: Multiple default methods in an interface may use a private method to perform common logging before or after an operation.
  • Formatting: Different interface methods that return formatted data may use a private method for common formatting logic.
  • Common Calculations: Multiple default methods may perform different operations while sharing a common calculation implemented in a private method.
  • API Development: Interface designers use private methods to keep helper logic internal while exposing only the required public API methods.
Comment