Variance
๋ค์ด๊ฐ๋ฉฐ
- ์ ๋ค๋ฆญ์ ๋ค๋ฃจ๊ฒ ๋๋ฉด ๋งค๋ฒ๋ด๋ ์ดํด๊ฐ ์ ์๋๋ ๋จ์ด๋ค์ด ๋งค๋ฒ ๋ฑ์ฅํ๋ค.
- ๋ณ์ฑ, ๊ณต๋ณ, ๋ฐ๊ณต๋ณ, ๊ทธ๋ฆฌ๊ณ ๋ฌด๊ณต๋ณ
- ์ญ์ ์ ๋๋ก ์ดํดํ๊ธฐ ์ํด ์ ๋ฆฌํฉ๋๋ค :)
๊ฐ๋ณ์ฑ(Variance)์ด๋?
Variance refers to how subtyping between more complex types relates to subtyping between their components.
- Google Translate
- Variance๋ ๋ ๋ณต์กํ ์ ํ ๊ฐ์ ํ์ ์ ํ์ด ํด๋น ๊ตฌ์ฑ ์์ ๊ฐ์ ํ์ ์ ํ๊ณผ ๊ด๋ จ๋๋ ๋ฐฉ์์ ๋ํ๋ ๋๋ค.
- ์ญ์ ์ดํด๊ฐ ์๋๋ ๊ฐ์ฅ ์์ ๋จ์ด์ธ subtype์ ๋ํด ์์๋ณด์.
Subtyping์ด๋?
a subtype is a datatype that is related to another datatype (the supertype) by some notion of substitutability
-
A๋ผ๋ Data type์ด B๋ผ๋ Data type์ ๋์ฒดํ ์ ์์ ๊ฒฝ์ฐ
- ์ด๋ A๋ B์ Subtype์ด๋ผ๊ณ ๋งํ ์ ์๋ค.
- B๋ A์ Supertype์ด๋ผ๊ณ ๋งํ ์ ์๋ค.
- ์ญํ ์ ๋์ฒด๊ฐ ๊ฐ๋ฅํ์ง๊ฐ Subtype์ ์ค์ํ ํฌ์ธํธ์ธ๊ฑฐ ๊ฐ๋ค.
- Liskov substitution principle
- Category Theory
-
๊ฐ๋จํ ์๋ฅผ ๋ค์ด๋ณด์.
-
bird๋ผ๋ ํด๋์ค๊ฐ ์กด์ฌํ๊ณ ํ์์ duck, cuckoo, ostrich๋ผ๋ ํด๋์ค๊ฐ ์กด์ฌํ๋ค.
-
duck, cuckoo, ostrich๋
bird๋ผ๋ ๊ณ ์ ์ ํน์ฑ
์ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ ๊ฐ์์ ํน์ฑ์ ๊ฐ์ง๊ณ ์๋ค. -
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ bird์ duck, cuckoo, ostrich๋
supertype - subtype
์ ๊ด๊ณ๋ฅผ ๊ฐ์ง๊ณ ์๋ค.- ์ด ๊ด๊ณ๋
duck, cuckoo, ostrich <: bird
๋ก ํ๊ธฐํ ์ ์๋ค. - ์ฌ๊ธฐ๊น์ง๋ ์ดํด๊ฐ ์ ๋๋ค.
- ์ด ๊ด๊ณ๋
-
์ด์ ์ฝ๋๋ฅผ ํตํด ์ดํดํด๋ณด์.
- ์๋์ ๊ทธ๋ฆผ์ Java์ Collection Subtyping ๊ด๊ณ๋๋ฅผ ํํํ ๊ฒ์ด๋ค.
Cat <: Animal
์ ๊ด๊ณ์ผ๋- ๊ทธ๋ฆผ์ ๋ณด๋ฉด
List<Cat> <: List<Animal>
์ด ์ฑ๋ฆฝํ์ง ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
- ๊ทธ๋ฆผ์ ๋ณด๋ฉด
์ ์์ฐ์ค๋ฝ์ง ๋ชปํ์ฌ ์ดํดํ๊ธฐ ์ด๋ ค์ด ๊ฒ์ผ๊น?
-
์๋ฅผ ๋ค์ด Animal์ ์ฌ๋ฌ๊ฐ์ง์ Subtype์ ๊ฐ์ง๊ณ ์๋ค๊ณ ๊ฐ์ ํด๋ณด์
- Dog, Cat, Bird์ ๊ฐ์ Subtype์ด ์๋ค.
-
์๋์ ์ฝ๋๋ ์๋ฌด ๋ฌธ์ ๊ฐ ์๋ค.
- Animal์ Subtype์ธ Dog, Cat, Bird๋ ๋์ฒด ๊ฐ๋ฅํ๋ค.
List<Animal> animals = new ArrayList<>();
animals.add(dog); // dog
animals.add(cat); // cat
animals.add(bird); // bird
์กฐ๊ธ ๋ ํ์ฅํด์ List<Cat>
๋ List<Animal>
์ Subtype์ธ๊ฐ?
-
์ฆ
๋์ฒด๊ฐ๋ฅํ๊ฐ?
๋ผ๋ ๊ด์ ์์ ๋ฐ๋ผ๋ณด์ -
์๋์ ์ฝ๋๋ ์ปดํ์ผ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
- ์ ์ปดํ์ผ ์๋ฌ๊ฐ ๋ฐ์ํ ๊น?
List<Cat> cats = new ArrayList<>();
cats.add(smallCat);
cats.add(bigCat);
List<Animal> animals = cats // compile error
List<Cat>
์ ๋ฐฐ์ด์ Heap ๋ฉ๋ชจ๋ฆฌ์ ์์ฑํ๋ฉฐ ์ฃผ์๋ฅผ ํ ๋น๋ฐ๋๋ค.List<Animal> = List<Cat>
์ด ๊ฐ๋ฅํ๊ฒ ๋๋ค๋ฉด?- ๋ ๊ฐ์
List
๋ ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๊ฐ์ง๊ฒ๋๋ค. - ์ด๋
List<Animal>
์Dog
๋ฅผ ๋ฃ๊ฒ ๋๋ฉด ์ด๋ป๊ฒ ๋ ๊น?- Dog๋ Animal์ Subtype์ด๊ธฐ ๋๋ฌธ์ Dog๋
List<Animal>
์ ์ถ๊ฐ๊ฐ ๋๋ค. - ๊ฐ์ ์ฃผ์๋ฅผ ๊ฐ์ง๊ณ ์๋
List<Cat>
๋ ์๋ฑํDog
๊ฐ ๋ค์ด์ Type์ด ๊นจ์ง๊ฒ ๋๋ ์ํฉ์ด ๋ฐ์ํ๋ค. - ํ์ ์์ ์ฑ์ ์ํด Java์์๋ ๊ณต๋ณ์ ํ์ฉํ์ง ์๋๋ค.
- ๋ฐ๋ ์ํฉ(
List<Animal> <: List<Cat>
)๋ ํ์ฉํ์ง ์๋๋ค. - Reference
- Dog๋ Animal์ Subtype์ด๊ธฐ ๋๋ฌธ์ Dog๋
ํ์ง๋ง ์ด๋ฐ ์ํฉ์?
List<Animal>
์ด ๋จ์ํ ์ฝ๊ธฐ๋ง ํ๋ค๋ฉด?- ์ฆ ๋ณ๊ฒฝํ์ง ์๋ ์ํฉ์ด๋ผ๋ฉด ํ์ ์์ ์ฑ์ด ๊นจ์ง ์ด์ ๊ฐ ์์ง ์์๊น?
- ์ด๋ ๊ฒ ์ฌ์ฉํ๋ฉด ๋ ํธํ์ง ์์๊น?
- Java์ Wildcard๋ฅผ ํตํด ํด๊ฒฐํ ์ ์๋ค.
๊ฐ๋ณ์ฑ(Variance) ์ข ๋ฅ์ ๋ํด
- ๊ณต๋ณ(Covariant)
- Subtype์ ํ์ฉํ์ง๋ง SuperType์ ํ์ฉํ์ง ์๋ ๊ฒฝ์ฐ
- ๋ฐ๊ณต๋ณ(Contravariant)
- SuperType์ ํ์ฉํ์ง๋ง Subtype์ ํ์ฉํ์ง ์๋ ๊ฒฝ์ฐ
- Bivariant
- SuperType, Subtype ๋ ๋ค ํ์ฉํ๋ ๊ฒฝ์ฐ
- ๋ฌด๊ณต๋ณ(Invariant)
- SuperType, Subtype ๋ ๋ค ํ์ฉํ์ง ์๋ ๊ฒฝ์ฐ
Reference
- https://asuraiv.tistory.com/17?category=813980
- https://cla9.tistory.com/44?category=814455
- http://happinessoncode.com/2017/05/22/java-generic-and-variance-2/
- https://woowacourse.github.io/javable/post/2020-11-09-generics-basic/
- https://medium.com/mj-studio/%EC%BD%94%ED%8B%80%EB%A6%B0-%EC%A0%9C%EB%84%A4%EB%A6%AD-in-out-3b809869610e
- https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html
- https://docs.oracle.com/javase/tutorial/java/generics/subtyping.html
- https://docs.oracle.com/javase/tutorial/java/generics/nonReifiableVarargsType.html
- https://dzone.com/articles/covariance-and-contravariance