[Java] The use of Java private interface methods

This article is for learning reference only!

Related tutorial address:

https://www.baeldung.com/java-interface-private-methods

https://www.geeksforgeeks.org/private-methods-java-9-interfaces/

https://www.runoob.com/java/java9-private-interface-methods.html

private interface methods

An interface is a contract that defines a set of methods and their signatures. It can be extended by any class and the methods implemented in that class. Since Java 9, it is possible to have private methods in interfaces .

Since a private method is only accessible within the interface in which it is defined, you can take advantage of such methods to write sensitive code that you don't want to be accessible by any class or interface.

This article discusses private interface methods in Java and how to implement them.

What are private interface methods in Java?

In Java, methods in interfaces are **public by default. ** This allows any class or interface extending this interface to call this method. The Java programming language allows the following in interfaces:

  • constant variable
  • abstract method
  • default method
  • static method
  • private method
  • private static method

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

Interface methods are public by default. That is, they can be accessed by the class implementing the interface, as well as any other class in the same package (or subpackage). However, interfaces can also declare methods as private.

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

It also makes it easier to ensure that the implementation of a method does not depend on the implementation of other classes or objects. Private interface methods are very helpful in reducing complexity and improving the readability of your codebase.

This means you cannot access methods outside of its defined interface. Private interface methods are not even visible to other interfaces - if you want other types (interfaces and classes) to have access to interface methods, you have to make them public . Private interface methods cannot be inherited by subclasses, nor can they be overridden in subclasses.

What are the benefits of private interface methods?

Here are some benefits of using private interface methods:

  • Code reusability - Developers can take advantage of private interface methods to reuse code within the declared interface; however, you may want to hide between implementations of the interface.
  • Encapsulation - Programmers can take advantage of private interface methods to encapsulate code that you do not wish to share between interface implementations.

Rules for Using Private Methods in Java Interfaces

Following are the rules and best practices that developers should follow while using private methods in Java applications

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

How to write private interface methods in Java

The following code sample illustrates how to create a private interface method in Java:

interface TestInterface {
    
    
    public abstract void abstractMethodExample();

    public
    default void defaultMethodExample() {
    
    
        privateMethodExample();
        privateStaticMethodExample();
        System.out.println("在默认方法内部\n");
    }

    private void privateMethodExample() {
    
    
        System.out.println("在私有非静态方法内部\n");
    }

    private static void privateStaticMethodExample() {
    
    
        System.out.println("在私有静态方法内部\n");
    }
}

See the interface named TestInterface shown in the preceding code listing . Private static and non-static methods are called from a default method called defaultMethodExample .

A class named TestClass implements this interface. Note how the abstract method is 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 message is displayed:

在私有非静态方法内部
在私有静态方法内部
在默认方法内部
抽象方法的实现内部

A private interface method in Java cannot be abstract

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

private abstract void privateMethodExample() {
    
    
        System.out.println("在私有方法内部\n");
    }
private abstract static void privateStaticMethodExample() {
    
    
        System.out.println("在私有静态方法内部\n");
    }

Note that we only added the abstract keyword to the method signatures of the two private methods of the interface named TestInterface . Here is the full source code for the interface named TestInterface after these changes :

interface TestInterface {
    
    
    public abstract void abstractMethodExample();
    public
    default void defaultMethodExample() {
    
    
        privateMethodExample();
        privateStaticMethodExample();
        System.out.println("在默认方法内部\n");
    }
    public static void staticMethodExample() {
    
    
        privateStaticMethodExample();
        System.out.println("静态方法内部\n");
    }
    private abstract void privateMethodExample() {
    
    
        System.out.println("在私有方法内部\n");
    }
    private abstract static void privateStaticMethodExample() {
    
    
        System.out.println("在私有静态方法内部");
    }
}

When compiling, the source code will fail to compile successfully, and the following error message will be displayed:

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

This proves that you cannot use both abstract and private keywords in a method signature .

END

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

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

Guess you like

Origin blog.csdn.net/m0_47015897/article/details/131412922
Recommended