[java] 인터페이스의 구현체를 다른 인터페이스로 형 변환하는 방법은?
예를 들어, 다음과 같이 구현체를 다른 인터페이스로 형 변환할 수 있습니다:
public interface InterfaceA {
void methodA();
}
public interface InterfaceB {
void methodB();
}
public class MyClass implements InterfaceA, InterfaceB {
public void methodA() {
System.out.println("This is methodA");
}
public void methodB() {
System.out.println("This is methodB");
}
}
public class Main {
public static void main(String[] args) {
MyClass myClass = new MyClass();
InterfaceA interfaceA = myClass; // InterfaceA로의 형 변환
InterfaceB interfaceB = (InterfaceB) myClass; // InterfaceB로의 형 변환
}
}
위 코드에서 MyClass
는 InterfaceA
와 InterfaceB
를 구현한 클래스입니다. main
메서드에서 MyClass
인스턴스를 InterfaceA
와 InterfaceB
로 형 변환하는 것을 볼 수 있습니다.
이렇게 함으로써 구현체를 다른 인터페이스로 형 변환할 수 있습니다.