Inheritance(계속)

Creating a subclass

constructor 문제 -> 객체를 생성하면 constructor이 호출됨 마치 C++에서의 initializer과 동일함

만약 constructor가 없다면 default constructor가 호출됨, 이건 superclass의 constructor을 호출하게 됨

class Employee{
	private String name;
    private int salary;
    public Employee(){
		name="NoName";
        salary=50000;
}
	public String getname() {return this.name;}
    public int getSalary() {return this.salary;}
}

class Manager extends Employee{
	private int bonus;
    public void setBonus(int bonus) {this.bonus=bonus;}
    public int getSalary() {return super.getSalary() +this.bonus;}
}

만약 subclass에서 constructor을 호출하였다면 superclass의 constructor가 호출된다음 subclass의 constructor가 실행된다. 따라서 정리하자면 superclass constructor은 무조건 호출되게 되어 있음.

class Employee {
	private String name;
	private int salary;
	public Employee() {
		this.name = "NoName";
		this.salary = 50000;
	}
	public void setName(String name) { this.name = name; }
	public String getName() { return this.name; }
	public int getSalary() { return this.salary; }
}
class Manager extends Employee {
	private int bonus;
	public Manager() {
		super.setName("NoName(Manager)");
		bonus = 10000;
	}
	public void setBonus(int bonus) { this.bonus = bonus; }
	public int getSalary() { return super.getSalary() + this.bonus; }
}

만약 superclass의 constructor가 매개변수를 받으면 어떻게 될까? 만약 subclass에서 superclass의 constructor을 호출하지 않으면 에러가 뜨게 된다. 왜냐하면 매개변수가 없는 constructor을 subclass에서 호출하려고 하기 때문이다. 그렇기 때문에 아래 코드와 같이 반드시 super(name, salary)를 호출하여야 한다.

class Employee {
	private String name;
	private int salary;
	public Employee(String name, int salary) {
		this.name = name;
		this.salary = salary;
	}
	public String getName() { return this.name; }
	public int getSalary() { return this.salary; }
}
class Manager extends Employee {
	private int bonus;
	public Manager(String name, int salary) {
    //필수적으로 superclass의 constructor을 호출하여 주어야 한다.
		super(name, salary);
		bonus = 10000;
	}
	public void setBonus(int bonus) { this.bonus = bonus; }
	public int getSalary() { return super.getSalary() + this.bonus; }
}

Detour : Implicit Constructor(혹은 Default Constructor)

constructor을 포함하고 있지 않은 클래스라면 Implicit constructor라고 부른다. Implicit constructor은 매개변수가 없고 오직 superclass constructor을 호출하는 역활을 한다.

만약 constructor가 포함되었다면 explicit constructor라고 부르며 더 이상 Implicit constructor는 호출되지 않는다.

class Employee {
	private String name;
	private int salary;
}

https://images.velog.io/images/tonyhan18/post/b5cb6927-58c5-47dc-9c8f-24408bd64284/image.png

class Employee {
	private String name;
	private int salary;
	public Employee(String name, int salary) {
		this.name = name;
		this.salary = salary;
	}
}

https://images.velog.io/images/tonyhan18/post/6ee5acb6-00c6-43ec-bbce-166c7d7eb8f9/image.png

그렇기에 explicit constructor가 존재하는 클래스에 매개변수 값을 넘기지 않으면 에러가 생긴다.

Dynamic method lookup

Employee <- Manager 인 상태에서 다음과 같은 코드가 있다고 하자

Manager boss = new Manager();
Employee empl = boss;
int salary = empl.getSalary();
// call getSalary which is defined in both Employee and Manager.