본문 바로가기

언어/Java

상속과 생성자

this 현재 객체
this() 매개변수가 다른 생성자 호출, 생성자의 first statement에 위치
super 현재 객체의 부모영역
 super() 부모생성자 호출, 생성자의 first statement에 위치
class ParentConstructor { // cf) class가 compile 될 때 아무것도 없으면 default 생성자 만들어질 것
	ParentConstructor(){	
		System.out.println("ParentConstructor()생성자가 호출됨");
	}
	ParentConstructor(String name) {
		System.out.println("ParentConstructor(" + name + ")생성자가 호출됨");
	}
}	

class ChildConstructor extends ParentConstructor {
	ChildConstructor(){
		// super();가 생략됨
		System.out.println("ChildConstructor()생성자가 호출됨");
	}
	
	ChildConstructor(int age){
		super("홍길동");	// 정확한 인자값을 전달하게 되면 생성자 중에서 매개변수를 갖는 생성자 호출됨
		System.out.println("ChildConstructor(" + age + ")생성자가 호출됨");
	}
}

// 사용자코드
public class InheritanceConstructor {

	public static void main(String[] args) {
		ParentConstructor p = new ParentConstructor(); // ParentConstructor()생성자가 호출됨
		ChildConstructor c = new ChildConstructor(); // 하위클래스타입의 객체 생성시 상위 클래스의 매개변수 없는 생성자가 자동 호출됨
													// ParentConstructor 먼저 호출하고 ChildConstructor 호출함
													// 결과: ParentConstructor()생성자가 호출됨, ChildConstructor()생성자가 호출됨
		System.out.println("--------------------------------------");
		ChildConstructor c1 = new ChildConstructor(10); //결과 : ParentConstructor(홍길동)생성자가 호출됨, ChildConstructor(10)생성자가 호출됨
	}

}

결과
16번째 줄 super("홍길동") 없을 때


같은 패키지 코드

package com.my.customer.dto;

public class Person {
	protected String name;
	protected String address;
	
	public Person() {
		super();	// 생략가능
	}
	

	public Person(String name, String address) {
		super();	// 생략가능
		this.name = name;
		this.address = address;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String toString() {	
		return "name은 " + name + ", address는 " + address;
	}
	
}
package com.my.customer.dto;

public class Customer extends Person {
	private String id;
	private String pwd;
	
	// 매개변수 없는 생성자
	public Customer() {
		super();
	}

	// 매개변수 갖는 생성자
	public Customer(String id, String pwd) {
		super();
		this.id = id;
		this.pwd = pwd;
	}
	
	// 직접 만들기
	public Customer(String id, String pwd, String name, String address) {
//		this.id = id;
//		this.pwd = pwd;
		//위의 2줄 매개변수 갖는 생성자와 중복되니까 아래로 줄일 수 있음
		this(id, pwd);		
		this.name = name;		// 부모와 자식의 변수가 중복되면 정확히 부모를 지칭하기 위해 super.name쓰지만 지금 중복되지 않아서 굳이 super.name 안쓰고 this.name 써도 됨
		this.address = address;
		
		//this 생성자 호출 super 생성자 호출 둘다 생성자의 첫머리에 와야해서 둘 중 하나만 써야함. 둘 다 줄여쓸 수는 없다.
//		this(id, pwd);
//		super(name, address);

	}
	
	public String toString() {	// shapes[i] obj.toString(); 그림 참고, Person에 name, address toString
		return "id는 " + id + ", pwd는 " + pwd + ", " + super.toString();
	}
	
	
	// setter/getter
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	public static void main(String[] args) {
		Customer c = new Customer("id1", "p1", "n1", "a1");
		System.out.println(c);
	}
}

결과