반응형
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
package AM;
class OverPar{
int a = 10;
int b = 20;
OverPar() {
System.out.println("부모생성자");
}
void meth_1() {
System.out.println("부모 meth_1() 실행"+ a + "," + b );
}
void meth_2() {
System.out.println("부모 meth_2() 실행"+ a + "," + b);
}
}
class OverUncle{
int a = 10;
int b = 20;
OverUncle() {
System.out.println("아저씨생성자");
}
}
class OverChild extends OverPar{
int a = 100; //hiding
int c = 300;
OverChild() {
System.out.println("자식생성자");
}
void meth_1() { //overriding
System.out.println("자식 meth_1() 실행"+ a + "," + b+","+c);
}
void meth_3() {
System.out.println("자식 meth_3() 실행"+ a + "," + b+","+c);
}
}
public class OverridingMain {
public static void main(String[] args) {
OverPar pp = new OverPar();
System.out.println("-------------------------");
OverPar pc = new OverChild();
System.out.println("-------------------------");
//OverChild cp = new OverPar();
System.out.println("-------------------------");
OverChild cc = new OverChild();
System.out.println("pp:"+pp.a+","+pp.b); //+","+pp.c);
System.out.println("pc:"+pc.a+","+pc.b); //+","+pc.c);
System.out.println("cc:"+cc.a+","+cc.b+","+cc.c);
cc.c = 3333;
System.out.println("cc:"+cc.a+","+cc.b+","+cc.c);
OverPar pcc = cc;
System.out.println("pcc:"+pcc.a+","+pcc.b);//+","+pcc.c);
OverChild cpcc = (OverChild)pcc;
System.out.println("cpcc:"+cpcc.a+","+cpcc.b+","+cpcc.c);
//OverChild cpp = (OverChild)pp;
System.out.println(pp instanceof OverPar);
System.out.println(pp instanceof OverChild);
System.out.println(pcc instanceof OverPar);
System.out.println(pcc instanceof OverChild);
//System.out.println(pcc instanceof OverUncle);
System.out.println("pp===================================");
pp.meth_1();
pp.meth_2();
// pp.meth_3();
System.out.println("pc=====================================");
pc.meth_1();
pc.meth_2();
// pc.meth_3();
System.out.println("cc=====================================");
cc.meth_1();
cc.meth_2();
cc.meth_3();
System.out.println("pcc=====================================");
pcc.meth_1();
pcc.meth_2();
// pcc.meth_3();
System.out.println("cpcc=====================================");
cpcc.meth_1();
cpcc.meth_2();
cpcc.meth_3();
//여러 자녀들이 같은 부모를 상속받고있다면 + 자녀들이 메소드를 오버라이드 했다면
// 호출시 각 각 다른 값이 나온다.
}
}
|
cs |
결과
부모생성자
-------------------------
부모생성자
자식생성자
-------------------------
-------------------------
부모생성자
자식생성자
pp:10,20
pc:10,20
cc:100,20,300
cc:100,20,3333
pcc:10,20
cpcc:100,20,3333
true
false
true
true
pp===================================
부모 meth_1() 실행10,20
부모 meth_2() 실행10,20
pc=====================================
자식 meth_1() 실행100,20,300
부모 meth_2() 실행10,20
cc=====================================
자식 meth_1() 실행100,20,3333
부모 meth_2() 실행10,20
자식 meth_3() 실행100,20,3333
pcc=====================================
자식 meth_1() 실행100,20,3333
부모 meth_2() 실행10,20
cpcc=====================================
자식 meth_1() 실행100,20,3333
부모 meth_2() 실행10,20
자식 meth_3() 실행100,20,3333
반응형
'JAVA' 카테고리의 다른 글
상속시 호출순서에 따른 실행결과 변화 (0) | 2020.08.30 |
---|---|
자바/상속 클래스들의 호출 순서 (0) | 2020.08.30 |
배열1 /선언/생성/길이/인덱스/초기화 (0) | 2020.08.14 |
for문 while문 (0) | 2020.08.13 |
조건문if /switch문 (0) | 2020.08.12 |