서로 상속관계에 있는 클래스 사이에서 가능한 참조변수 형변환
- 참조형의 형변환은 저장된 값, 주소값이 변환되는 것은 아님
- 자식클래스타입에서 조상클래스타입
- 조상클래스타입에서 자식클래스타입
class Main{
public static void main(String[] args) {
tv t = new tv();
smart_tv st = (smart_tv) t; // 조상클래스를 자식클래스 타입을 형변환 (smart_tv) 생략 불가능
smart_tv_super sts = new smart_tv_super();
tv t3 = (tv)st; // 자식클래스를 조상클래스 타입으로 형변환
tv t2 = st; // 자식클래스를 조상클래스 타입으로 형변환 (tv) 생략 가능
}
}
class tv{
boolean power;
int channel = 0;
}
class smart_tv extends tv{
String text = "Smart";
void play(){};
}
class smart_tv_super extends tv{
String text = "Super Smart";
}
class Main{
public static void main(String[] args) {
tv t = null;
smart_tv st = new smart_tv();
smart_tv sts = null;
System.out.println(st.text); // 출력 : "Smart"
t = st;
System.out.println(t.text); //에러
sts = (smart_tv)t;
System.out.println(sts.text); //출력 : "Super Smart"
}
}
class tv{
boolean power;
int channel = 0;
}
class smart_tv extends tv{
String text = "Smart";
void play(){};
}
class smart_tv_super extends tv{
String text = "Super Smart";
}