728x90
반응형
package chap7;
/*
* super 예약어
* super() 생성자 : 부모클래스의 생성자 호출
* super 참조변수 : 부모클래스의 객체를 참조하는 변수
*/
class Parent {
int x = 10;
void method() {
System.out.println("Parent 클래스의 method()");
}
}
class Child extends Parent {
int x = 20;
void method() {
int x = 30;
super.method(); //부모클래스의 메서드 호출
System.out.println("x=" + x); //10
System.out.println("this.x=" + this.x); //10
System.out.println("super.x=" + super.x); //10
}
}
public class SuperEx1 {
public static void main(String[] args) {
Child c = new Child();
c.method();
}
}728x90
반응형
'study > Java' 카테고리의 다른 글
| [Java] 22. 상속 Exam1 (SutdaCard 20장으로 이루어진 SutdaDeck 클래스 구현하기) (0) | 2022.03.21 |
|---|---|
| [Java] 22. super 생성자 (0) | 2022.03.21 |
| [Java] 22. 오버로딩, 오버라이딩 예제 (0) | 2022.03.21 |
| [Java] 22. 상속1 (0) | 2022.03.21 |
| [Java] 19 ~ 21. TEST5 풀이 (원(Circle) 클래스 구현하기) (0) | 2022.03.18 |