TIL

TIL - 230816

nayeonee__ 2023. 8. 16. 19:03

다형성

public class Application{
	public static void main(String[] args){
			Hunam human = new Human();
			Asian asian = new Asian();
			Human test = new Asian();
				test.greeting();

				Human rin = human; // == Human rin = new Human();t
		}
}

class Human{
		void showInfo(){
		System.out.println("All human begins are equal.");
	}

		void talk(){
			System.out.println("say something");
	}
}

class Asian extends Human{
		void showInfo(){
			System.out.println("Rin is an Asian ><b");
	}
		
		void greeting(){
			System.out.println("Hello ><b");

}

}

  • test 라는 오브젝트
    • Human 타입이다
      • 타입 부분은 옵션을 결정한다.
        • 옵션은 메서드를 의미한다.
    • 하지만 객체화는 Asian으로 되어있다.
      • 객체화되는 부분은 어디쪽 메서드를 호출할지 결정
        • 중복되어있는 메서드들이 있다면 객체화된 부분의 메서드가 호출된다.
  • 즉 test 는 Human 타입이고 Asian으로 객체화가 되었다.

'TIL' 카테고리의 다른 글

TIL - 230815  (0) 2023.08.15
TIL - 230810  (1) 2023.08.11
TIL -230808  (0) 2023.08.11
TIL - 230807  (0) 2023.08.11
TIL - 230804  (0) 2023.08.11