super() : 참조변수 부모호출
`
Parent.java
package kr.or.kihd.superr;
public class Parent {
int x = 100;
// public Parent() {
// System.out.println("조상클래스 생성자 호출");
// }
public Parent(int x) {
System.out.println("조상클래스 매개변수가 있는 생성자 호출");
}
public void show() {
System.out.println("조상클래스의 show() 호출");
}
}
Child.java
package kr.or.kihd.superr;
public class Child extends Parent {
int x = 20;
public Child() {
//super() : 조상클래스의 기본생성자를 호출.
// (기본생성자에 한해서만)생략하더라도 컴파일러가 알아서 추가함.
//조상클래스의 매개변수가 있는 생성자를 호출시에는 생략을 절대로 하면 안한다.
//=>조상클래스가 먼저만들어져야하기 떄문임.
super(200);
System.out.println("자손클래스 생성자 호출");
}
@Override
public void show() {
System.out.println("현재 인스턴스의 x = " + x);
System.out.println("자손클래스의 this.x = " + this.x);
System.out.println("조상클래스의 super.x = " + super.x);
super.show();
}
}
SuperTest.java
package kr.or.kihd.superr;
public class SuperTest {
public static void main(String[] args) {
//자손클래스 인스턴스 생성
//조상클래스의 인스턴스 생성이 제일 먼저 이루어지고,
//자손클래스의 인스턴스가 생성되어 합쳐진 상태로 힙에 할당된다.
Child child = new Child();
child.show();
System.out.println();
Parent parent = new Parent(1000);
parent.show();
System.out.println();
Parent parent1 = new Child(); //다형성
parent1.show();
}
}