Language/Java

[JAVA 23] 자바 super ( 슈퍼 )

reifier.tistory.com 으로 이전하였습니다. 2018. 2. 13. 15:56
반응형


■ super

1. 정의 : 

' 부모의 객체 ' 또는 ' 부모의 생성자 ' 를 지칭하는 대명사

2. 사용

1) 이름이 같은 ' 부모객체 '의 멤버변수를 접근할 때

2) 오버라이딩 전의 ' 부모객체 ' 의 메소드를 접근할 때

3) ' 부모의 생성자 ' 를 호출할 때


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class SuperS1{               // super Study 1 , 부모 객체
    String name = "부모";
 
    SuperS1(String name){
        this.name = name;
    }
 
    void m(){
        System.out.println("4. SuperS1 : 부모");
    }
}
 
class SuperChild extends SuperS1{    // 자식 객체
    String name = "junior";
 
    SuperChild(){
        super("자식");      // new SuperS1("자식");
    }
 
    void m(){              // 오버라이딩 ( Overriding )
        System.out.println("3. SuperChild : 자식");
    }
 
    void call(){
        System.out.println("1. this.name : " + this.name);        // 자신의 멤버 변수
        System.out.println("2. super.name : " + super.name);    // 부모의 멤버 변수
        m();            // 자신의 메소드
        super.m();        // 부모의 메소드
    }
}
 
class SuperUser{
    public static void main(String[] args){
        SuperChild sc = new SuperChild();
        sc.call();
    }
}
cs


순서가 왜 저렇게 나왔는지는 void call(){} 부분을 잘 보고 생각해 보기를 바랍니다.

반응형