업다운캐스팅
업캐스팅(upcasting) : 서브클래스 객체를 슈퍼클래스 타입으로 변환
ex)
class Person {}
class Student extends Person {}
Person p = new Student(); 업캐스팅
cf)
Student s = new Student();
Person p = s; 레퍼런스만 치환해주면 됨.
업캐스팅된 s는 슈퍼클래스의 멤버만 접근 가능하다.
2. 다운캐스팅(downcasting) : 슈퍼클래스 객체를 서브클래스 타입으로 변환
ex)
class Person{}
class Student extends Person{}
Person p = new Person();
Student s = (Student)p; //캐스팅 필요
3. instanceof 연산자 - if문과 같이 쓴다
-상속받은 클래스는 타입 판단이 어려움 -> instanceof 연산자 활용
ex)
s intanceof Student >>> True
p instanceof Person >>> False
'Programming > Java' 카테고리의 다른 글
5장. 상속 #3 오버라이딩 (0) | 2020.06.12 |
---|---|
Troubleshoot #1 No enclosing instance of type ... is accessible (0) | 2020.06.11 |
5장. 상속 #1 슈퍼/서브클래스 (0) | 2020.06.11 |
4장. 클래스와 객체 #5 접근지정자 #6 static과 non-static (0) | 2020.06.10 |
4장. 클래스와 객체 #3 객체 배열 #4 메소드 (0) | 2020.06.10 |
댓글