정보처리기사 실기JavaJava - 추상클래스와 인터페이스난이도 5SHORT_ANSWER

정보처리기사 실기 Java - 추상클래스와 인터페이스 기출문제 #168

문제

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

Java
abstract class Shape { String type = "Shape"; abstract int area(); String getType() { return type; } } class Rect extends Shape { String type = "Rect"; int w, h; Rect(int w, int h) { this.w = w; this.h = h; } int area() { return w * h; } String getType() { return type; } } public class Main { public static void main(String[] args) { Shape s = new Rect(3, 4); System.out.println(s.type + " " + s.getType() + " " + s.area()); } }

정답

Shape Rect 12

ShapeRect12

해설

s.type은 선언 타입 Shape의 필드이므로 "Shape". s.getType()은 오버라이딩된 Rect의 메서드가 호출되어 "Rect". s.area()는 Rect의 area()가 호출되어 3×4 = 12. 필드 은닉(hiding)과 메서드 오버라이딩의 차이를 묻는 문제이다.

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

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