11.15 Analyze the following code: ``` public class Test { public static void main(String[] args) { new B(); } } class A { int i = 7; public A() { setI(20); System.out.println("i from A is " + i); } public void setI(int i) { this.i = 2 * i; } } class B extends A { public B() { // System.out.println("i from B is " + i); } public void setI(int i) { this.i = 3 * i; } } ``` A. The constructor of class A is not called. B. The constructor of class A is called and it displays "i from A is 7". C. The constructor of class A is called and it displays "i from A is 40". **D. The constructor of class A is called and it displays "i from A is 60".** 저는 출력값이 40이라고 생각했습니다. B 객체가 만들어지기 위해서는 A라는 생성자가 호출되어야 하고 그러면 A내에서 있는 SetI(20)은 2*20=40이 된다고 생각했는데, B의 SetI()를 이용하는 것 같습니다. 위의 상속의 경우, A먼저 그리고 B의 순서로 객체가 만들어지는 건 아닌지요?
# 메소드 오버라이딩 ## B의 `setI()` 메소드가 호출 된 이유 만들어진 객체가 B 클래스의 인스턴스(객체)이고, B클래스에서 setI() 메소드를 재정의 했기 때문입니다. ``` public static void main(String[] args) { new B(); } ``` ## A먼저 그리고 B의 순서로 객체가 만들어지는 건 아닌지요? 맞습니다. A의 인스턴스가 먼저 만들어지고 이를 포함하는 B의 인스턴스가 최종적으로 만들어 집니다. 그러나 B의 인스턴스가 만들어 지지 않았다고 하여 메소드가 오버라이딩 되지 않는 것은 아닙니다. 실제로 메소드는 인스턴스 내부에 존재하는 것이 아니라 클래스 영역에 존재하기 때문입니다. (참조: http://wanzargen.tistory.com/16)