2012년 12월 29일 토요일

[Ch10Ex06] 상속에서 생성자의 연속적인 호출

Result :

 Point2D 생성자
 Point3D 생성자


서브 클래스인 Point3D로 객체를 생성하였는데 결과 화면을 보면 자신의 생성자 뿐만 아니라 수퍼 클래스의 디폴트 생성자도 호출됨을 알 수 있다. 그것도 출력 결과가 Point2D생성자 가 출력되고 Point 3D 생성자가 나중에 출력됨. 고로 수퍼 클래스의 디폴트 생성자가 먼저 생성된후, 서브 클래스의 생성자가 호출된다는 것을 알 수 있다.


Sample Code

File : Point2D

package kame.ch10.ex06;

public class Point2D {
   protected int x;
   protected int y;
   public Point2D( ){
  System.out.println( " Point2D 생성자 ");
   }
}


File : Point3D

package kame.ch10.ex06;

public class Point3D extends Point2D {
protected int z=30;
public Point3D( ){
System.out.println( " Point3D 생성자 ");
}
}


File : Ch10Ex06

import kame.ch10.ex06.Point3D;

public class Ch10Ex06 {
  public static void main(String[] args) {
Point3D  pt = new Point3D();
  }
}

Reference book : 이클립스로 배우는 Java 6 Programming/ 임종권, 전병찬/ 가메출판사

댓글 없음:

댓글 쓰기