Select Page

Working with Private Interface Methods in Java

Joydip Kanjilal
Published: August 24, 2022

Java Developer Tutorials

An interface is a contract that defines a set of methods and their signatures. It can be extended by any class and its methods implemented in that class. Beginning with Java 9, you can have private methods in interfaces.

Since private methods are only accessible within the interface in which it has been defined, you can take advantage of such methods to write sensitive code which you would not want to be accessed by any class or interface.

This programming tutorial presents a discussion on private interface methods in Java and how to implement them.

Looking to learn Java in a classroom or online course setting? We have a list of the Best Online Courses to Learn Java that can help get you started.

What are Private Interface Methods in Java?

In Java, a method in an interface is public by default. This allows this method to be called by any class or interface extending this interface. The Java Programming Language allows the following to be used in interfaces:

  • Constant variables
  • Abstract methods
  • Default methods
  • Static methods
  • Private methods
  • Private Static methods

A private interface method is a special type of Java method that is accessible inside the declaring interface only. This means that no class that extends the interface can access this method directly using an instance of the class.

Interface methods are public by default. That is, they can be accessed by classes that implement the interface, as well as any other class in the same package (or sub packages). However, an interface may declare a method private as well.

Private interface methods allow you to explicitly state that a method is not meant to be used by other classes, interfaces or objects. This can be very helpful when writing code, as it allows you to keep your codebase organized and readable.

It also makes it easier to ensure that the implementation of a method does not rely on implementation of other classes or objects. Private interface methods can be very helpful in reducing complexity and improving readability of code bases.

This means that you cannot access the method outside of its defining interface. Private interface methods are not visible even to other interfaces – if you want an interface method to be accessible by other types (interfaces and classes), you must make it public. Private interface methods cannot be inherited by subclasses or overridden in subclasses either.

What are the Benefits of Private Interface Methods?

Below are some of the benefits of using private interface methods:

  • Code re-usability – Developers can leverage private interface methods to reuse code inside the declaring interface; however, you would want to hide across implementations of the interface.
  • Encapsulation – Programmers can take advantage of private interface methods to encapsulate code that you would not want to be shared across implementations of the interface.

Read: Working with Functional Interfaces in Java

Rules For Using Private Methods in Interfaces in Java

Below are the rules and best practices developers should follow when using private methods in Java applications

  • Abstract methods are not allowed in private interfaces. Private interface methods can only be used inside interfaces.
  • It is not possible to have both private and abstract modifiers at the same time.
  • A static method can be used inside a static or non-static method.
  • It is not possible to use a private non-static method within a private static method.

How to Program Private Interface Methods in Java

The following code example illustrates how you can create private interface methods in Java:

interface TestInterface {
    public abstract void abstractMethodExample();

    public
    default void defaultMethodExample() {
        privateMethodExample();
        privateStaticMethodExample();
        System.out.println("Inside a default method\n");
    }

    private void privateMethodExample() {
        System.out.println("Inside a private non-static method\n");
    }

    private static void privateStaticMethodExample() {
        System.out.println("Inside a private static method\n");
    }
}

Refer to the interface named TestInterface shown in the preceding code listing. The private static and non-static methods are called from the default method named defaultMethodExample.

The class named TestClass implements this interface. Note how the abstract method has been implemented in this class:

public class TestClass implements TestInterface {
    @Override
    public void abstractMethodExample() {
        System.out.println
        ("Inside the implementation of an abstract method");
    }
    public static void main(String[] args) {
        TestInterface test = new TestClass();
        test.defaultMethodExample();
        test.abstractMethodExample();
    }
}

When you execute the program, the following text messages will be displayed:

Inside a private non-static method
Inside a private static method
Inside a default method
Inside the implementation of an abstract method

Read: The Best Tools for Remote Developers

Private Interface Methods in Java Cannot Be Abstract

We know that private interface methods cannot be abstract. Let’s understand and verify this with an example. Update the source code of the two private methods of the TestInterface from our previous example, as shown below:

private abstract void privateMethodExample() {
        System.out.println("Inside a private method\n");
    }
private abstract static void privateStaticMethodExample() {
        System.out.println("Inside a private static method");
    }

Note that we have added only the abstract keyword in the method signature of both the private methods of the interface named TestInterface. Here is the complete source code of the interface named TestInterface after these changes:

interface TestInterface {
    public abstract void abstractMethodExample();
    public
    default void defaultMethodExample() {
        privateMethodExample();
        privateStaticMethodExample();
        System.out.println("Inside a default method\n");
    }
    public static void staticMethodExample() {
        privateStaticMethodExample();
        System.out.println("Inside a static method\n");
    }
    private abstract void privateMethodExample() {
        System.out.println("Inside a private method\n");
    }
    private abstract static void privateStaticMethodExample() {
        System.out.println("Inside a private static method");
    }
}

When you compile, the source code will not compile successfully and the following error message will be displayed:

TestClass.java:17: illegal combination of modifiers: abstract and private

This proves that you are not allowed to use both abstract and private keywords together in the method signature.

Final Thoughts on Private Interface Methods in Java

Private interface methods are a feature of Java that allows developers to define private methods (both static and non-static) in an interface. This is useful for defining helper methods that can be called from inside the declaring interface only.

In addition to increasing code reusability inside interfaces, private interface methods allow us to expose only the intended method implementations. Such methods are exclusive to the interface in which they are defined and cannot be accessed or inherited from any other class or interface.

Read more Java programming tutorials and guides to software development.

Source: www.developer.com