문제
다음 Java 코드의 실행 결과를 쓰시오.
Javaabstract class Animal { abstract void sound(); void info() { sound(); System.out.print(" Animal"); } } interface Flyable { default void fly() { System.out.print("Flying "); sound(); } void sound(); } class Bird extends Animal implements Flyable { public void sound() { System.out.print("Chirp"); } public void fly() { System.out.print("Soaring "); super.info(); } } public class Main { public static void main(String[] args) { Flyable f = new Bird(); f.fly(); } }
정답
Soaring Chirp Animal
SoaringChirpAnimalSoaring Chirp Animal
해설
Bird 객체에서 f.fly() 호출 시 Bird의 오버라이딩된 fly() 메서드가 실행됩니다. 이 메서드는 "Soaring "을 출력하고 super.info()를 호출합니다. super는 상위 클래스 Animal을 가리키므로 Animal의 info() 메서드가 호출되어 sound() 메서드를 호출하고 " Animal"을 출력합니다. sound()는 추상 메서드이므로 Bird의 구현체가 호출되어 "Chirp"을 출력합니다.