예, 비공개 방법 또는 비공개 정적 메소드 Java 9의 인터페이스에서 이러한 방법을 사용하여 코드 중복성을 제거할 수 있습니다. 비공개 방법 해당 인터페이스 내에서만 유용하거나 액세스할 수 있습니다. 한 인터페이스에서 다른 인터페이스 또는 클래스로 개인 메서드에 액세스하거나 상속할 수 없습니다.
구문
interface <interface-name> {
private static void methodName() {
// some statements
}
private void methodName() {
// some statements
}
} 예
interface Java9Interface {
public abstract void method1();
public default void method2() {
method4();
method5();
System.out.println("Inside default method");
}
public static void method3() {
method5(); // static method inside other static method
System.out.println("Inside static method");
}
private void method4() { // private method
System.out.println("Inside private method");
}
private static void method5() { // private static method
System.out.println("Inside private static method");
}
}
public class PrivateStaticMethodTest implements Java9Interface {
@Override
public void method1() {
System.out.println("Inside abstract method");
}
public static void main(String args[]) {
Java9Interface instance = new PrivateStaticMethodTest();
instance.method1();
instance.method2();
Java9Interface.method3();
}
} 출력
Inside abstract method Inside private method Inside private static method Inside default method Inside private static method Inside static method