Java 简明教程

Java - Private Interface Methods

在 Java 9 中引入了私有和静态私有接口方法。作为一个私有方法,此类方法无法通过实现类或子接口来访问。这些方法被引入以允许使用封装,某些方法的实现将仅保留在 interface 中。它有助于减少重复、提高可维护性并编写简洁的代码。

Private and static private interface methods were introduced in Java 9. Being a private method, such a method cannot be accessed via implementing class or sub-interface. These methods were introduced to allow encapsulation where the implementation of certain methods will be kept in the interface only. It helps to reduce duplicity, increase maintainability, and to write clean code.

Interface Prior to Java 8

在 Java 8 之前,接口只能有抽象方法和常量 variables 。因此,实现类必须实现相同的内容。请看下面的示例。

Prior to Java 8, interface can have only abstract method and constant variables. So implementing class has to implement the same. See the below example.

Example

package com.tutorialspoint;

interface util {
   public int sum(int a, int b);
}

public class Tester implements util {
   public static void main(String[] args) {
      Tester tester = new Tester();
      System.out.println(tester.sum(2, 3));
   }

   @Override
   public int sum(int a, int b) {
      return a + b;
   }
}

Output

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

5

在以上示例中,我们可以看到实现类必须实现该方法,因为它实现了接口。

In the above example, we can see that implementing class has to implement the method as it is implement the interface.

Default Method in Interface from Java 8

借助 Java 8,引入了默认方法,我们可以在其中提供该方法的默认实现,并且实现类无需实现相同的内容。引入此功能是为了促进 Lambda 表达式,其中现有的集合框架可以与新引入的功能性接口一起使用,而无需实现接口的所有方法。这有助于避免重新编写集合框架。请参见以下示例:

With Java 8, default methods were introduced where we can provide the default implementation of the method and implementing class is not needed to implement the same. This feature was introduced to facilitate lambda expression where existing collection framework can work with newly introduced functional interfaces without implementing all the methods of the interfaces. This helped in avoiding re-writing of collection framework. See the below example:

Example

package com.tutorialspoint;

interface util {
   public default int sum(int a, int b) {
      return a + b;
   }
}

public class Tester implements util {
   public static void main(String[] args) {
      Tester tester = new Tester();
      System.out.println(tester.sum(2, 3));
   }
}

Output

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

5

Private Method in Interface from Java 9

从 Java 9 开始,接口得到了进一步增强,可以包含私有方法。自 Java 9 起,我们现在可以在接口中使用私有方法和私有静态方法。这有助于封装功能,并有助于保持方法的完整性。由于私有方法不能被继承,因此它们可以在接口的公共方法中使用,如下面的示例所示:

From java 9, interfaces are enhanced further to have private methods. Now Java 9 onwards, we can have private as well as private static methods in interface. This helps in encapsulating the functionality and helps to keep integrity of the method. As private method cannot be inherited, they can be used in public methods of the interface as shown below example:

Example

package com.tutorialspoint;

interface util {
   public default int operate(int a, int b) {
      return sum(a, b);
   }
   private int sum(int a, int b) {
      return a + b;
   }
}

public class Tester implements util {
   public static void main(String[] args) {
      Tester tester = new Tester();
      System.out.println(tester.operate(2, 3));
   }
}

Output

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

5

Private Static Method in Interface from Java 9

同样,我们还可以使用私有静态方法,可以从静态和非静态方法进行调用。请参见以下示例:

Similarly, we can have private static method which can be called from static and non-static methods. See the example below:

Example

package com.tutorialspoint;

interface util {
   public default int operate(int a, int b) {
      return sum(a, b);
   }
   private static int sum(int a, int b) {
      return a + b;
   }
}

public class Tester implements util {
   public static void main(String[] args) {
      Tester tester = new Tester();
      System.out.println(tester.operate(2, 3));
   }
}

Output

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

5

私有静态方法不能在接口中调用非静态方法,并且在接口外部不可访问。即使是实现类也不能访问私有静态方法。

A private static method cannot call non-static method in an interface and it is not accessible outside the interface. Even implementing class cannot access the private static method.