목차

[Java] Polymorphism

  • description : [Java] Polymorphism
  • author : 오션
  • email : shlim@repia.com
  • lastupdate : 2022-03-18

[Java] Polymorphism

객체지향의 사실과 오해 역할, 책임, 협력 관점에서 본 객체지향

조영호 (지은이) 위키북스 7쇄 발행 2022년 01월 27일 (1쇄 발행, 2015-06-17 )

● 책임을 수행하는 방법은 자율적으로 선택할 수 있다

요청을 받은 사람들은 요청을 처리하는 방법을 자유롭게 선택할 수 있다. 커피 제조를 요청받은 바리스타는 자신만의 독특한 방법으로 커피를 제조할 수 있다. 어떤 바리스타는 카푸치노의 거품을 이용해 커피 표면에 아름다운 무늬를 만들기도 하고 어떤 바리스타는 아메리카노의 향을 좀 더 향기롭게 만드는 방법을 알지도 모른다.중요한 것은 커피를 제조하라는 동일한 요청을 받더라도 바리스타의 역할을 수행하는 사람들마다 서로 다른 방식으로 요청을 처리할 수 있다는 것이다.

이처럼 동일한 요청에 대해 서로 다른 방식으로 응답할 수 있는 능력을 다형성(polymorphism)이라고 한다.

Java Polymorphism

Polymorphism means “many forms”, and it occurs when we have many classes that are related to each other by inheritance.


Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class.
Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.


For example, think of a superclass called Animal that has a method called animalSound().
Subclasses of Animals could be Pigs, Cats, Dogs, Birds
And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.):



polymorphism, 폴리모피즘
→ 다형성 (그리스어: 여러 개의 형태. poly(하나 이상), morph(형태)

다형성(多形性)
→ 같은 종(種)의 생물이면서도 어떤 형태나 형질이 다양하게 나타나는 현상. 예를 들어 암수에 따라 크기, 형태, 색깔 따위가 차이나는 것이다.
출처 : 네이버 사전


Polymorphism

Polymorphism is the presentation of one interface for multiple data types.

For example, integers, floats, and doubles are implicitly polymorphic:
regardless of their different types, they can all be added, subtracted, multiplied, and so on.

In the case of OOP, by making the class responsible for its code as well as its own data,
polymorphism can be achieved in that each class has its own function that (once called) behaves properly for any object.

Ref

Polymorphism
Java Polymorphism