super

yanju·2022년 11월 26일
0
post-thumbnail

super는 자식 클래스에서 부모 클래스로부터 상속받은 멤버를 참조하는데 사용한다.

상속받은 멤버와 자식의 클래스에 정의된 멤버의 이름이 같을 때는 super를 사용해서 구별할 수 있다.

public class SuperTest {

    public static void main(String[] args) {
        Child c = new Child();
        c.method();;
    }
}

class Parent {
    int x = 10;
}

class Child extends Parent {
    int x = 20;

    void method() {
        System.out.println("x " + x);  // 20
        System.out.println("this.x " + this.x);  // 20
        System.out.println("super.x " + super.x);  // 10
    }
}

0개의 댓글

관련 채용 정보