인터페이스는 Java에서 다른 인터페이스를 구현할 수 없습니다.
- Java의 인터페이스는 본질적으로 특별한 종류의 클래스입니다. 클래스와 마찬가지로 인터페이스에는 메서드와 변수가 포함됩니다. 클래스와 달리 인터페이스는 항상 완전히 추상적입니다.
- 인터페이스는 인터페이스 키워드를 제외하고는 클래스처럼 정의됩니다. 클래스 대신에 인터페이스에서 선언된 변수는 정적 입니다. 및 최종 인터페이스에 정의된 메서드는 공개 추상입니다. 방법.
- 인터페이스는 확장할 수 있습니다 인터페이스는 여러 개이지만 하나의 인터페이스는 구현할 수 없습니다. 인터페이스가 구현된 경우 해당 메서드를 정의해야 하고 인터페이스에는 메서드 정의가 없기 때문에 다른 인터페이스입니다.
- 다른 인터페이스로 인터페이스를 구현하려고 하면 Java에서 컴파일 타임 오류가 발생합니다.
예
interface MainInterface {
void mainMethod();
}
interface SubInterface extends MainInterface { // If we put implements keyword in place of extends, // compiler throws an error.
void subMethod();
}
class MainClass implements MainInterface {
public void mainMethod() {
System.out.println("Main Interface Method");
}
public void subMethod() {
System.out.println("Sub Interface Method");
}
}
public class Test {
public static void main(String args[]) {
MainClass main = new MainClass();
main.mainMethod();
main.subMethod();
}
} 출력
Main Interface Method Sub Interface Method