자바의 정석 문제풀이

Book_366_7_20

요옫 2021. 8. 19. 17:48

상속

 

class Parent1{

int x=100;

 

void method() {

System.out.println("Parent Method");

}

}

 

 

class Child1 extends Parent1{

int x=200;

 

void method() {

System.out.println("x="+x);  //this.x와 같다

System.out.println("super.x= "+super.x);

System.out.println("this.x= "+this.x);

}

}

 

 

public class Book_366_7_20 {

 

public static void main(String[] args) {

 

Parent1 p=new Child1();

Child1 c=new Child1();

 

System.out.println("p.x= "+p.x);

p.method();

System.out.println();

System.out.println("c.x= "+c.x);

c.method();

}

}

 

//결과

p.x= 100

x=200

super.x= 100

this.x= 200

 

c.x= 200

x=200

super.x= 100

this.x= 200

'자바의 정석 문제풀이' 카테고리의 다른 글

Book_364_7_18  (0) 2021.08.19
Book_368_7_21  (0) 2021.08.19
Book_227_5_23  (0) 2021.08.10
Book_218_5_19(2차원배열+과목별 점수)  (0) 2021.08.10
Book_197_5_06(배열의 최대값,최소값)  (0) 2021.08.09