정보처리기사 실기JavaJava - 추상클래스 동적 디스패치난이도 4SHORT_ANSWER

정보처리기사 실기 Java - 추상클래스 동적 디스패치 기출문제 #1462

문제

다음 Java 코드의 실행 결과를 쓰시오.

Java
interface Device { String brand = "Generic"; default String getBrand() { return "Interface"; } String getModel(); } abstract class Electronics { String brand = "Electronics"; String getBrand() { return this.brand + "-" + getCategory(); } abstract String getCategory(); } class Laptop extends Electronics implements Device { String brand = "Laptop"; String getModel() { return "LT-2024"; } String getCategory() { return "Computer"; } String getBrand() { return super.brand + "-" + Device.super.getBrand() + "-" + this.brand; } } public class Main { public static void main(String[] args) { Device d = new Laptop(); Electronics e = (Electronics) d; System.out.println(d.brand + " " + e.getBrand() + " " + d.getModel()); } }

정답

Generic Electronics-Interface-Laptop LT-2024

GenericElectronics-Interface-LaptopLT-2024

해설

d.brand는 인터페이스 필드로 정적 바인딩되어 "Generic"입니다. e.getBrand()는 Laptop 클래스의 오버라이딩된 메서드가 호출되어 super.brand(Electronics의 brand) + Device.super.getBrand() + this.brand = "Electronics" + "-" + "Interface" + "-" + "Laptop"이 됩니다. d.getModel()은 동적 바인딩으로 Laptop의 구현이 호출되어 "LT-2024"입니다.

이런 문제 20~50개를 한 번에 풀어보세요

매번 새로 추가되는 모의고사 + 오답 자동 복습 + 회차별 실력 추적. 회원가입 후 무료 이용.