15.6 What is the output of running class Test? ``` public class Test { public static void main(String[] args) { new Circle9(); } } public abstract class GeometricObject { protected GeometricObject() { System.out.print("A"); } protected GeometricObject(String color, boolean filled) { System.out.print("B"); } } public class Circle9 extends GeometricObject { /** Default constructor */ public Circle9() { this(1.0); System.out.print("C"); } /** Construct circle with a specified radius */ public Circle9(double radius) { this(radius, "white", false); System.out.print("D"); } /** Construct a circle with specified radius, filled, and color */ public Circle9(double radius, String color, boolean filled) { super(color, filled); System.out.print("E"); } } ``` A. ABCD B. BACD C. CBAE D. AEDC **E. BEDC** 1. 메인메소드에서 new Circle9()를 생성하니, 2. 부모 클래스인 GeometricObject를 생성해야 한다. 근데 왜 B가 생성되고, CDE순서가 아니라 EDB 순서로 출력이 되는지 궁금합니다.
# 메소드의 실행흐름 ## 요약 메소드의 호출이 끝나면 실행의 흐름은 해당 메소드를 호출 했던 곳으로 돌아가게 됩니다. ## 설명 먼저 메인 메소드에서 생성자를 호출합니다. ``` public class Test { public static void main(String[] args) { new Circle9(); // 생성자 호출 } } ``` 그럼 생성자 코드를 확인해봐야겠네요. 아래 코드를 보면 생성자의 첫줄에서 또 다른 생성자인 `this(1.0)`를 호출 하는 것을 확인 할 수 있습니다. ``` public class Circle9 extends GeometricObject { /** Default constructor */ public Circle9() { this(1.0); // 또 다른 생성자 호출 System.out.print("C"); } ... } ``` 따라서 실행의 흐름은 this(1.0)으로 넘어가게 됩니다. 여기서도 마찬가지로 해당 생성자의 첫 줄에서 또다르 생성자를 호출 하고 있습니다. ``` public class Circle9 extends GeometricObject { ... /** Construct circle with a specified radius */ public Circle9(double radius) { this(radius, "white", false); // 또 다른 생성자 호출 System.out.print("D"); } ... } ``` 계속해서 다음 생성자로 실행의 흐름이 넘어가게 됩니다. 드디어 이곳에서 부모 클래스의 생성자를 호출 합니다. ``` public class Circle9 extends GeometricObject { ... /** Construct a circle with specified radius, filled, and color */ public Circle9(double radius, String color, boolean filled) { super(color, filled); // 부모 클래스의 생성자 호출 System.out.print("E"); } } ``` 해당 생성자에서 "B"라는 문자열을 출력 합니다. 그리고 더 이상 수행 할 내용이 없기 때문에 실행의 흐름은 해당 생성자를 호출 했던 곳으로 돌아가게 됩니다. 위 쪽에 있는 코드들로 말이죠. 따라서 "BEDC"가 출력이 되겠습니다. ``` public abstract class GeometricObject { ... protected GeometricObject(String color, boolean filled) { System.out.print("B"); // B } } ```
# 생각 거리 만약 부모클래스의 생성자를 호출하는 영역이 없었다면 "AEDC" 가 출력이 되었을 겁니다. 왜냐하면 부모클래스를 직접적으로 호출 하지 않으면 부모클래스의 디폴트 생성자가 호출되기 때문 입니다. ``` public class Circle9 extends GeometricObject { ... /** Construct a circle with specified radius, filled, and color */ public Circle9(double radius, String color, boolean filled) { // 부모클래스를 직접적으로 호출 하지 않으면 부모클래스의 디폴트 생성자가 호출 // super(color, filled); System.out.print("E"); } } ```