-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_24_Example2.java
More file actions
35 lines (31 loc) · 1.12 KB
/
_24_Example2.java
File metadata and controls
35 lines (31 loc) · 1.12 KB
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
package chap_07;
// 1) 조상 클래스
class Phone7 {
String name;
String type;
int capacity;
int price;
}
// 2) 자손 클래스
class UpgradedPhone9 extends Phone7 {
void useStandBy() {}
void useNameDrop() {}
}
public class _24_Example2 {
public static void main(String[] args) {
// 3) 참조 변수 및 객체 생성
Phone7 p = null;
UpgradedPhone9 u1 = new UpgradedPhone9();
UpgradedPhone9 u2 = null;
// 4-1) 자손 타입의 참조 변수로 같은 타입의 객체 멤버를 호출할 경우
u1.useStandBy();
// 4-2) 자손 타입의 참조 변수를 조상 타입의 참조 변수로 형변환할 경우
p = u1; // p = (Phone7)u1;
// 4-3) 조상 타입의 참조 변수로 자손 타입의 객체 멤버를 호출할 경우
// p.useStandBy(); -> 에러 발생
// 4-4) 조상 타입의 참조 변수를 자손 타입의 참조 변수로 형변환할 경우
u2 = (UpgradedPhone9)p;
// 4-5) 자손 타입의 참조 변수로 같은 타입의 객체 멤버를 호출할 경우
u2.useStandBy();
}
}