JAVA 15. Upcasting, Downcasting and instanceof

less than 1 minute read

Chapter 5 ~ 7  
Ch 5 14 15 16 17
Ch 6 18 19 20  
Ch 7 21 22    
Prev Section Next Section
   

Upcasting, Downcasting and instanceof

Upcasting

  • 업캐스팅
    • 서브클래스의 레퍼런스를 슈퍼클래스의 레퍼런스에 대입
    • 슈퍼클래스 레퍼런스로 서브클래스 객체를 가리키게 되는 현상
    class Person { ... }
    class Student extends Person { ... }
      
    Person p;
    Student s = new Student();
    p = s; //업캐스팅
    

Downcasting

  • 다운캐스팅
    • 슈퍼클래스 레퍼런스를 서브클래스 레퍼런스에 대입
    • 업캐스팅 된 것을 다시 원래대로 복귀
    • 반드시 명시적 형변환으로 지정
    class Person { ... }
    class Student extends Person { ... }
      
    Person p = new Student("허고컴"); //업캐스팅
    Student s = (Student)p; //다운캐스팅
    

instanceof

  • instanceof 연산자
    • 레퍼런스가 가리키는 객체의 타입 식별
    ObjectReference instanceof ClassType //true or false
    
  • 예시 0504
    Person p = new Professor();
      
    if(p instanceof Person) //true
    if(p instanceof Student)  //false
    if(p instanceof Researcher) //true
    if(p instanceof Professor)  //true
      
    if("자바싫어" instanceof String)  //true
      
    if(3 instance of int) //문법 오류 : int는 객체가 아님
    

Thanks for watching my post!

Categories:

Updated:

Comments