Skip to content

Commit

Permalink
[Week3][Chap10] 다형성과 캐스팅 (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Feb 5, 2024
1 parent 7a345c9 commit cb4af2e
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package week3.poly.basic;

public class CastingMain1 {
public static void main(String[] args) {
// 부모 변수가 자식 인스턴스 참조(다형적 참조)
Parent poly = new Child(); // x001
// 단 자식의 기능은 호출할 수 없다.
// poly.childMethod() // compile error!!

// 다운 캐스팅(부모 타입 -> 자식 타입)
Child child = (Child) poly; // x001
child.childMethod();
}
}

0 comments on commit cb4af2e

Please sign in to comment.