쌍용강북교육센터/7월
0722_Ex02_Inheritance : 상속
호쏘2
2021. 7. 22. 21:22
package ex0722;
public class Ex02_Inheritance {
public static void main(String[] args) {
Sample2 ss = new Sample2();
ss.disp();
}
}
class Test2 {
int a=10;
int b=20;
public void print() {
System.out.println(a+":"+b);
}
}
class Sample2 extends Test2 {
int b=100;
int x=200;
int y=300;
public void disp() {
int x=50;
System.out.println("a:"+a); // 10, super 클래스의 a필드
System.out.println("b:"+b); // 100, 자신클래스의 b필드
System.out.println("super.b:"+super.b); // 20, super 클래스의 b필드
System.out.println("x:"+x); // 50, 지역변수
System.out.println("this.x:"+this.x); // 200, 자신클래스의 x필드
System.out.println("y:"+y); // 300, 자신클래스의 y필드
}
}
this.a // 내 자신의 필드 a
super.a // 아버지의 필드 a
this(10); // 자신의 클래스에서 다른 생성자를 호출
super();
super(10);