- 객체 자신을 가리키는 참조변수, 인스턴스 메서드(생성자) 내에서만 존재 (static 메서드 내에 사용불가능) - 조상의 멤버를 자신의 멤버와 구별할 때 사용
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);
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child();
child.method();
}
}
출력
class Parent {
int x = 10; //super.x
}
class Child extends Parent{
void method(){
System.out.println("x:"+x);
System.out.println("this.x:"+this.x);
System.out.println("super.x:"+super.x);
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child();
child.method();
}
}
출력
ch7-11 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 z){
super(x,y); // 조상 클래스의 생성자 Point(int x, int y) 호츨
this.z=z; // 자신의 멤버 초기화
}
}
public class Main {
public static void main(String[] args) {
Point3D point3D = new Point3D(2,3,4);
System.out.println(point3D.x);
System.out.println(point3D.y);
System.out.println(point3D.z);
}
}
출력
class Point3D extends Point{
int z;
Point3D(int x, int y , int z){
this.x=x;
this.y=y;
this.z=z; // 자신의 멤버 초기화
}
super()을 사용하지 않고 참조변수 this를 사용하면
- 생성자의 첫 줄에 반드시 생성자를 호출해야 한다. 그렇지 않으면 컴파일러가 생성자의 첫줄에 super(); 을 삽입
class Point{
int x,y;
Point(){
this(0,0);
}
Point(int x, int y){
this.x=x;
this.y=y;
}
}
위의 코드는 아래와 같다.
class Point extends Object{
int x,y;
Point(){
this(0,0); // 생성자 호출
}
Point(int x, int y){
super(); //Object 생성자 호출
this.x=x;
this.y=y;
}
}
생성자의 첫 줄에 생성자를 호출하지 않은 경우 )
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() 컴파일러가 삽입 -> Point();를 호출
this.x=x;
this.y=y;
this.z=z;
}
//조상의 getLocation()을 오버라이딩
String getLocation(){
return "x:" + x + " y:"+y +" z:"+z;
}
}
public class Main {
public static void main(String[] args) {
Point3D p = new Point3D(1,2,3);
System.out.println(p.getLocation());
}
}
해당 오류가 발생한다.
해결방법 1)
class Point {
int x;
int y;
Point(){ //기본 생성자 작성
}
String getLocation(){
return "x:" + x + " y:"+y;
}
}
class Point3D extends Point{
int z;
Point3D(int x, int y ,int z){
this.x=x;
this.y=y;
this.z=z;
}
String getLocation(){
return "x:" + x + " y:"+y +" z:"+z;
}
}
public class Main {
public static void main(String[] args) {
Point3D p = new Point3D(8,5,3);
System.out.println(p.getLocation());
}
}
해결방법 2)
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); //조상의 생성자 Point(int x, int y)를 호출
this.z=z;
}
String getLocation(){
return "x:" + x + " y:"+y +" z:"+z;
}
}
public class Main {
public static void main(String[] args) {
Point3D p = new Point3D(8,5,3);
System.out.println(p.getLocation());
}
}