다형성: Polymorphism

  • Super class 타입의 변수가 Sub class 타입의 객체를 참조할 수 있다!
  • 예시) Notebook class가 Computer class를 상속 받고 있다고 가정
Computer computer = new Notebook("Bravo", "Intel", 4, 240, 2/4, 15)
  • Computer 타입의 변수 computer가 Notebook 타입의 객체를 참조하고 있음!

  • 다음의 예를 생각해보자
  • 가정) Computer와 Notebook class는 모두 toString method를 지니고 있음
System.out.println(compter.toString());
  • 위 예에서의 toString은 두 class 중 어느 class의 method를 호출하게 될까?
  • 답은 Notebook class의 toString !
    • 즉, 동적 바인딩(dynamic binding)이 일어남

      • Runtime 중에 참조 객체를 결정
    • cf. Computer class의 toString을 호출하는 경우, 정적 바인딩
      • 컴파일러가 참조 객체를 결정


'Software Convergence > Java' 카테고리의 다른 글

Object 클래스와 Wrapper 클래스  (0) 2018.11.13
static과 접근제한자  (0) 2018.10.19
상속의 단점과 Strategy Pattern  (0) 2018.09.03
Generic 자료형 실습  (0) 2018.07.12
instanceof / encapsulation  (0) 2018.07.04

+ Recent posts