Java 참조변수 super, 생성자 super()
by 볼빵빵오춘기참조변수 super
- 조상의 멤버를 자신의 멤버와 구별할 때 사용
- 객체 자신을 가리키는 참조변수. 인스턴스 메서드(생성자) 내에만 존재 ⇒ static메서드내에서는 사용불가
※ 참고
super ⇒ 조상멤버, 자신멤버 구별
this ⇒ lv와 iv 구별에 사용
예제 코드1
더보기
![](https://blog.kakaocdn.net/dn/bYYgk1/btsA30x4Za9/6pEM3MGQeZi9iUkhCKy7Fk/img.png)
class Hello{
public static void main(String args[]){
Child c = new Child();
c.method();
}
}
class Parent{ int x = 10; /* super.x */ }
class Child extends Parent{
int x = 20; // this.x
void method(){
System.out.println("x = " + x);
System.out.println("this.x = "+this.x);
System.out.println("super.x = "+super.x);
}
}
x=20
this.x = 20
super.x = 10
![](https://blog.kakaocdn.net/dn/bYYgk1/btsA30x4Za9/6pEM3MGQeZi9iUkhCKy7Fk/img.png)
예제 코드2
더보기
class Hello{
public static void main(String args[]){
Child c = new Child();
c.method();
}
}
class Parent2{ int x = 10; /* super.x, this.x 둘 다 사용가능 */ }
class Child extends Parent2{
void method(){
System.out.println("x = " + x);
System.out.println("this.x = "+this.x);
System.out.println("super.x = "+super.x);
}
}
x = 10
this.x = 10
super.x = 10
super() - 조상의 생성자 (≠ super 관계짓지말기!)
- 조상의 생성자를 호출할 때 사용한다.
(알고가야할 점! 상속에서는 생성자, 초기화블럭은 상속이 되지않는다!) - 조상의 멤버는 조상의 생성자를 호출해서 초기화!
더보기
class Point{
int x,y;
Point(int x, int y){
this.x = x;
this.y = y;
}
}
class Point3D extends Point{
int z;
Point3D(int x, int y, int y){
this.x = x; // 조상의 멤버를 초기화
this.y = y; // 조상의 멤버를 초기화
this.z = z;
}
}
⇒ Point3D(int x, int y, int y) 에서 구현부를 보면 x,y 조상 멤버까지 초기화를 한다.
이렇게 써서 에러는 나지않을 수 있지만 이렇게 쓰지않는다.
자손의 생성자는 자기가 선언한 멤버만 초기화를 해야한다.
따라서 아래 코드와 같이 변경해야한다.
Point3D(int x, int y, int y){
super(x,y); // 조상클래스의 생성자 Point(int x, int y)를 호출
this.z = z; // 자신의 멤버를 초기화
}
- 생성자의 첫 줄에 반드시 생성자를 호출해야 한다. (⇒ super(), this()를 호출)
더보기
why? 그렇지 않으면 컴파일러가 생성자의 첫 줄에 super();를 삽입하기 때문이다.
class Point{
int x;
int y;
Point(){
this(0,0);
}
Point(){
this.x = x;
this.y = y;
}
}
위의 코드를 컴파일 하면 아래 코드와 같이 바뀐다.
class Point extends Object{
int x;
int y;
Point(){
this(0,0);
}
Point(){
super(); // Object();
this.x = x;
this.y = y;
}
}
예제코드
더보기
![](https://blog.kakaocdn.net/dn/zqxbs/btsA7QVLGrU/TccB1Iw1e0w9Gqhl1Z98GK/img.png)
public class Hello {
public static void main(String[] args) {
Point3D p3 = new Point3D(1,2,3);
System.out.println(p3.getLocation());
}
}
class Point{
int x;
int y;
Point(int x, int y){
this.x = x;
this.y = y;
}
String getLocation() {
return "x : " + x + ", y : "+y;
}
}
class Point3D extends Point{
int z;
Point3D(int x, int y,int z){
// super(x,y);
this.x = x;
this.y = y;
this.z = z;
}
String getLocation() {
return "x : " + x + ", y : "+y + ", z : "+z;
}
}
![](https://blog.kakaocdn.net/dn/zqxbs/btsA7QVLGrU/TccB1Iw1e0w9Gqhl1Z98GK/img.png)
⇒
'모든 생성자는 첫 줄에 다른 생성자를 호출해야한다.' 를 불충족시키기때문이다.
따라서 아래 코드와 같이 적는다.
public class Hello {
public static void main(String[] args) {
Point3D p3 = new Point3D(1,2,3);
System.out.println(p3.getLocation());
}
}
class Point{
int x;
int y;
Point(int x, int y){
// super() // 가 생략된것 // Object();
this.x = x;
this.y = y;
}
String getLocation() {
return "x : " + x + ", y : "+y;
}
}
class Point3D extends Point{
int z;
Point3D(int x, int y,int z){
super(x,y);
this.z = z;
}
String getLocation() {
return "x : " + x + ", y : "+y + ", z : "+z;
}
}
this, this(), super, super() 정리하자면
super()
자손 클래스 생성자에서 조상 멤버를 초기화하기 위함.
this()
같은 클래스 내 한 생성자에서 다른 생성자 호출할 때 사용.
super
객체 자신을 가리키는 참조변수. 조상 멤버와 자신의 멤버 구분할 때 사용.
this
객체 자신을 가리키는 참조변수. 지역변수와 인스턴스 변수 구분할 때 사용.
'👩🏻💻 About 프로그래밍 > Java' 카테고리의 다른 글
Java import문, import문 선언, static import문 (0) | 2023.11.29 |
---|---|
Java 패키지(package), 패키지 선언, 클래스패스(classpath) (0) | 2023.11.29 |
Java 오버라이딩(overriding) (0) | 2023.11.29 |
Java 단일상속(Single Inheritance), Object 클래스 (0) | 2023.11.29 |
Java 클래스 간의 관계, 상속과 포함관계 (0) | 2023.11.29 |
블로그의 정보
Hello 춘기's world
볼빵빵오춘기