자격증/정보처리기사 실기 - 기출문제

[정보처리기사 실기] 2020년 2회 05 - 프로그래밍 [JAVA]

쏠솔랄라 2025. 7. 11. 15:16

 

 


05 다음 JAVA로 구현된 프로그램을 분석하여 괄호에 들어갈 알맞은 답을 쓰시오.

class Parent {
	void show() {System.out.println("parent");}
}
class Child extends Parent {
	void show() {System.out.println("child");}
}
public class Test {
	public static void main(String[] args){
    	Parent pa = (  괄호  ) Child();
        pa.show();
    }
}

 

 

 

 

 

 

해설

코드 실행순서 및 해석
1 class Parent {    
2      void show() {System.out.println("parent");}    
3 }    
4 class Child extends Parent {   Parent 부모클래스 상속
5      void show() {System.out.println("child");}   자식 속성에서 재정의된다
6 }    
7 public class Test {    
8      public static void main(String[] args){    
9           Parent pa = (      ) Child();   [부모클래스명] [객체변수명] = new [자식클래스생성자()]
-> Child 클래스의 형 변환이 일어남 (재정의)
10           pa.show();   show()를 호출
-> Child 클래스의 show() 메서드를 호출한다
11     }    
12 }