static
메서드static
메서드는 인터페이스 자체와 연관된 동작을 제공하기 위해 사용되며, 객체 생성 없이 호출할 수 있다.
static
메서드처럼, 인터페이스의 static
메서드는 인터페이스 이름을 통해서만 호출할 수 있다. 객체를 통해 호출하는 것이 아니라, 인터페이스와 직접적으로 관련된 동작을 제공한다.static
메서드는 인터페이스를 구현하는 클래스에서 오버라이딩할 수 없다. 따라서 각 클래스에 상관없이 인터페이스 자체에 고정된 동작을 제공한다.interface InterA {
void method1();
// 인터페이스의 static 메서드
static void staticMethod() {
System.out.println("staticMethod 호출");
}
}
public class InterfaceMainEx07 {
public static void main(String[] args) {
// 인터페이스 이름으로 static 메서드 호출
InterA.staticMethod(); // 출력: "staticMethod 호출"
}
}
InterA
인터페이스를 구현한 클래스가 존재하더라도, staticMethod
는 오직 인터페이스 자체로만 호출된다.static
메서드는 클래스나 객체 상태와 무관하며, 인터페이스에 고정된 동작을 정의한다.private
메서드interface InterA {
void method1();
// 디폴트 메서드
default void method2() {
System.out.println("method2 호출");
method3(); // private 메서드 호출
}
// private 메서드 (내부적으로만 사용됨)
private void method3() {
System.out.println("method3 호출");
}
}
class ClassA implements InterA {
public void method1() {
System.out.println("method1 호출");
}
}
public class InterfaceMainEx08 {
public static void main(String[] args) {
ClassA a = new ClassA();
a.method1(); // 출력: "method1 호출"
a.method2(); // 출력: "method2 호출" -> "method3 호출"
}
}