앞에서 살펴봤듯이 this는 자기자신의 주소값을 가지고있는반면 super는 부모의 주소값을 가지게 된다.
만약 부모의 생성자를 사용해야하는경우에는 super()생성자를 이용하여 접근이가능하다.
하지만 실제적인 메모리 상에 올라가는 구조를 보자면 아래와같다.
class Man {//3.부모에있는 멤버변수,멤버메소드 메모리에올림
private String name;
public Man(String name) {
this.name = name;
}
public void tellYourName() {
System.out.println("My name is " + name);
}
}
class BusinessMan extends Man {//2.호출받음 하지만 상속받고있음 바로 부모쪽으로 올라가 부모를 메모리상에 올림
//4.부모의 멤버변수,멤버메소드가 올라간후 자신의 멤버변수,멤버메소드를 메모리상에올림
String company;
String position;
public BusinessMan(String name, String company, String position) {
super(name);//부모의 멤버변수가 올라가있는상태임으로this사용가능,하지만 this()생성자는 자신을 가르킴으로 주의 super사용할것
this.company = company;
this.position = position;
}
public void tellYourInfo() {
System.out.println("My company is " + this.company);
System.out.println("My position is " + this.position);
tellYourName();
}
}
class MyBusinessMan {
public static void main(String[] args) {
BusinessMan man =
new BusinessMan("Yoon","Hybrid ELD", "Staff Eng.");// 1.new BusinessMan 생성
man.tellYourInfo();
}
}