💻 Java 참조변수super. / 생성자super()
🎯 목표 : 상속관계의 클래스에서의 super 키워드 사용의 이해
📒 참조변수 super .
❗특징
- 객체 자신을 가르키는 참조변수
- 인스턴스 메소드 내에서만 존재.
- 각 인스턴스 변수명이 같아도 참조변수 super, this에 따라 값도 따로 할당됨.
👉 참조변수 super. 예제 _ 01
📌 부모클래스 Example / 자식클래스 ExampleChild / 자식,부모 메소드 add
- 자식클래스의 Overriding한 메소드 add는 인스턴스 매개변수 값으로 들어온 1,2,3을 계산하여 출력하기 위해 만들었다.
- return값을 보면, super.add(a,b) 부분이 있는데, super.은 부모클래스의 참조변수로서의 역할을 하며 부모클래스의 메소드 add에 매개변수 값을 Input 한다.
출력값
6
3
class Example {
int a;
int b;
Example (int a, int b) {
this.a = a;
this.b = b;
}
int add (int a, int b) {
System.out.println(a+b);
return a+b;
}
}
class ExampleChild extends Example {
int c;
ExampleChild(int a, int b, int c) {
super(a,b);
this.c = c;
}
int add () {
// 참조변수 super
System.out.println(a+b+c);
return super.add(a,b)+c;
}
}
public class extest {
public static void main(String[] args) {
ExampleChild t = new ExampleChild(1,2,3);
t.add();
}
}
👉 참조변수 super. 예제 _ 02
- 부모,자식 클래스의 인스턴스 변수명이 같더라도 다른값을 갖는걸 확인하기 위해 자식클래스에 같은 변수명 int타입의 a,b를 추가하였고, 생성자 초기화 값을 this를 사용하여 자식클래스 변수 a,b,c에 매개변수 값을 input하였다.
- 추가로 자식클래스 메소드 add에 부모클래스의 인스턴스 변수 a,b를 호출하는 메소드를 삽입하였다.
- 출력값은 0 6 으로 같은이름의 변수가 있다 하더라도 this와 super에 따라 다른값을 가지는것을 확인할수 있다.
- 자식클래스의 인스턴스변수 a,b를 제거하게되면 참조변수 this가 가르키는 자식클래스 내부에 인스턴스변수가 없어지게 되며, 참조변수 this는 부모클래스를 자동으로 참조하게되고 출력값은 3 6 이 될것이다.
class Example {
int a;
int b;
Example (int a, int b) {
this.a = a;
this.b = b;
}
public Example() {
}
int add (int a, int b) {
return a+b;
}
}
class ExampleChild extends Example {
int a;
int b;
int c;
ExampleChild(int a, int b, int c) {
super();
this.a = a;
this.b = b;
this.c = c;
}
int add () {
// 참조변수 super
System.out.println(super.add(super.a,super.b));
System.out.println(a+b+c);
return a+b+c;
}
}
public class extest {
public static void main(String[] args) {
ExampleChild t = new ExampleChild(1,2,3);
t.add();
}
}
📒 생성자 super()
❗특징
- 부모생성자를 호출할때 사용
- 부모클래스의 멤버는 부모생성자(super)를 호출하여 초기화 하는 것이 좋다.
- 생성자의 첫줄에 반듯이 부모생성자를 호출해야된다.
- 호출하지 않으면 컴파일러가 자동으로 super();생성자를 생성한다.
👉 생성자 super() 예제
📌 부모클래스 Example / 자식클래스 ExampleChild / 자식,부모 메소드 add
class Example {
int a;
int b;
Example (int a, int b) {
this.a = a;
this.b = b;
}
int add (int a, int b) {
System.out.println(a+b);
return a+b;
}
}
class ExampleChild extends Example {
int c;
ExampleChild(int a, int b, int c) {
super(a,b); // 생성자 super
this.c = c;
}
void add () {
// 참조변수 super
System.out.println(super.add(a,b)+c);
}
}
public class extest {
public static void main(String[] args) {
ExampleChild t = new ExampleChild(1,2,3);
t.add();
}
}
- 출력값 3 6
- 자식클래스 생성자의 super(a,b); 는 부모클래스 인스턴스 변수값을 초기화 해주고 있다.
ExampleChild(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
- 만약, 자식클래스 생성자 부분을 위 코드로 변경하게 되면 부모클래스의 생성자를 호출하고 있지 않기 때문에 에러가 발생할 것이고 부모클래스에 기본생성자를 추가하고나면 자식클래스의 생성자 부분의 맨 첫줄에 컴파일러가 부모클래스 생성자 super();를 자동으로 생성할 것이다.
- 에러 발생을 통하여 자식클래스 생성자의 맨 첫줄은 부모클래스 생성자 super()가 반듯이 들어가야 된다는것을 알수있다.
'Language > JAVA' 카테고리의 다른 글
Java Access Modifier(접근제어자) Encapsulation(캡슐화) - OOP_캡슐화 (0) | 2022.08.22 |
---|---|
Java Modifier(제어자) Access Modifier(접근제어자) - OOP_캡슐화 (0) | 2022.08.22 |
Java Overriding(오버라이딩) - OOP_상속 (0) | 2022.08.21 |
Java Class의 Inheritance(상속) / Composite(포함) - OOP_상속 (0) | 2022.08.20 |
Java Initialized(초기화) (0) | 2022.08.20 |