์๋
! ์ ๋ค๋ฆญ ์ฐ๊ตฌ๋ฅผ ๊ณ์ํฉ์๋ค. ๋น์ ์ ์ด๋ฏธ ์ด์ ์์
( ์ ๋ค๋ฆญ์ผ๋ก ์์
ํ ๋ varargs ์ฌ์ฉ ๋ฐ ์ ํ ์ญ์ ์ ๋ํ ์ ๋ณด)์์ ์ด์ ๋ํ ์๋นํ ์ง์์ ์ป์ ์ง๋ง ์์ง ๊ณ ๋ คํ์ง ์์ ์ค์ํ ์ฃผ์ ์ธ ์์ผ๋์นด๋ ๊ฐ ์์ต๋๋ค . ์ด๊ฒ์ ์ ๋ค๋ฆญ์ ๋งค์ฐ ์ค์ํ ๊ธฐ๋ฅ์
๋๋ค. ์ฐ๋ฆฌ๋ ๊ทธ๊ฒ์ ๋ํด ๋ณ๋์ ๋ ์จ์ ํ ์ ํ์ต๋๋ค! ์ฆ, ์์ผ๋์นด๋์ ๋ํด ํน๋ณํ ๋ณต์กํ ๊ฒ์ ์์ต๋๋ค. ๋ฐ๋ก ํ์ธํ ์ ์์ต๋๋ค :) ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
public class Main {
public static void main(String[] args) {
String str = new String("Test!");
// No problem
Object obj = str;
List<String> strings = new ArrayList<String>();
// Compilation error!
List<Object> objects = strings;
}
}
๋ฌด์จ ์ผ์ด์ผ? ์ฐ๋ฆฌ๋ ๋งค์ฐ ์ ์ฌํ ๋ ๊ฐ์ง ์ํฉ์ ๋ด
๋๋ค. ์ด ๊ฒฝ์ฐ String
๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด๋ก ์บ์คํธํฉ๋๋ค Object
. ์ฌ๊ธฐ์๋ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋ชจ๋ ๊ฒ์ด ์์๋๋ก ์๋ํฉ๋๋ค. ๊ทธ๋ฌ๋ ๋ ๋ฒ์งธ ์ํฉ์์ ์ปดํ์ผ๋ฌ๋ ์ค๋ฅ๋ฅผ ์์ฑํฉ๋๋ค. ํ์ง๋ง ์ฐ๋ฆฌ๋ ๊ฐ์ ์ผ์ ํ๊ณ ์์ต๋๋ค. ๊ทธ๋ ์ฃ ? ์ด๋ฒ์๋ ๋จ์ํ ์ฌ๋ฌ ๊ฐ์ฒด์ ์ปฌ๋ ์
์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. ๊ทธ๋ฐ๋ฐ ์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๊น? ์ฐจ์ด์ ์ด ๋ญ์ผ? String
ํ๋ ์ ๊ฐ์ฒด๋ฅผ ํ๋ Object
๋๋ 20๊ฐ์ ๊ฐ์ฒด ๋ก ์บ์คํ
ํฉ๋๊น ? ๊ฐ์ฒด ์ ๊ฐ์ฒด ๋ชจ์ ์ฌ์ด์๋ ์ค์ํ ์ฐจ์ด์ ์ด ์์ต๋๋ค . ํด๋์ค๊ฐ ํด๋์ค์ ํ์์ธ ๊ฒฝ์ฐ ๋ ์ ํ์๊ฐ ์๋๋๋ค . B
A
Collection<B>
Collection<A>
์ด๊ฒ์ด ์ฐ๋ฆฌ๊ฐ ์ฐ๋ฆฌ List<String>
๋ฅผList<Object>
. String
๋ ์ ํ์ Object
์ด์ง๋ง List<String>
์ ํ์๋ ์๋๋๋ค List<Object>
. ์ด๊ฒ์ ๋งค์ฐ ์ง๊ด์ ์ด์ง ์์ ๊ฒ์ฒ๋ผ ๋ณด์ผ ์ ์์ต๋๋ค. ์ธ์ด๋ฅผ ๋ง๋ ์ฌ๋๋ค์ ์ ์ด๋ ๊ฒ ๋ง๋ค์์๊น? ์ปดํ์ผ๋ฌ๊ฐ ์ฐ๋ฆฌ์๊ฒ ์ค๋ฅ๋ฅผ ์ฃผ์ง ์๋๋ค๊ณ ์์ํด ๋ด
์๋ค:
List<String> strings = new ArrayList<String>();
List<Object> objects = strings;
์ด ๊ฒฝ์ฐ ์๋ฅผ ๋ค์ด ๋ค์๊ณผ ๊ฐ์ด ํ ์ ์์ต๋๋ค.
objects.add(new Object());
String s = strings.get(0);
์ปดํ์ผ๋ฌ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์๊ณ List<Object>
๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ๋ฅผ ๋ง๋ค ์ ์๋๋ก ํ์ฉํ๊ธฐ ๋๋ฌธ์ ์ปฌ๋ ์
์ strings
์ด์ ๊ฐ์ฒด๋ฅผ ์ถ๊ฐํ ์ ์์ต๋๋ค ! ๋ฐ๋ผ์ ์ปฌ๋ ์
์ ์ผ๋ฐ ํ์ ํธ์ถ์ ํ์ ์ธ์๋ก ์ง์ ๋ ๊ฐ์ฒด ๋ง ํฌํจ๋๋ค๋ ๋ณด์ฅ์ด ์์ต๋๋ค . ์ฆ, ์ ๋ค๋ฆญ์ ์ฃผ์ ์ด์ ์ธ ํ์ ์์ ์ฑ์ ์์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ปดํ์ผ๋ฌ๊ฐ ์ด ์์
์ ์ค๋จํ์ง ์์๊ธฐ ๋๋ฌธ์ ๋ฐํ์์๋ง ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉฐ ์ด๋ ํญ์ ์ปดํ์ผ ์ค๋ฅ๋ณด๋ค ํจ์ฌ ๋์ฉ๋๋ค. ์ด์ ๊ฐ์ ์ํฉ์ ๋ฐฉ์งํ๊ธฐ ์ํด ์ปดํ์ผ๋ฌ๋ ๋ค์๊ณผ ๊ฐ์ ์ค๋ฅ๋ฅผ ํ์ํฉ๋๋ค. Object
strings
String
// Compilation error
List<Object> objects = strings;
List<String>
...๊ทธ๋ฆฌ๊ณ ์ ํ์์ด ์๋์ ์๊ธฐ์ํต๋๋ค List<Object>
. ์ด๊ฒ์ ์ ๋ค๋ฆญ์ ๋ํ ์๊ฒฉํ ๊ท์น์ด๋ฉฐ, ์ ๋ค๋ฆญ์ผ๋ก ์์
ํ ๋ ๊ธฐ์ตํด์ผ ํฉ๋๋ค. ๊ณ์ ๊ฐ์๋ค. ์์ ํด๋์ค ๊ณ์ธต ๊ตฌ์กฐ๊ฐ ์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
public class Animal {
public void feed() {
System.out.println("Animal.feed()");
}
}
public class Pet extends Animal {
public void call() {
System.out.println("Pet.call()");
}
}
public class Cat extends Pet {
public void meow() {
System.out.println("Cat.meow()");
}
}
๊ณ์ธต ๊ตฌ์กฐ๋ Pet์ ์ํด ์์๋๋ ๊ฐ๋จํ Animal ํด๋์ค์ ์ํด ๋งจ ์์ ์์ต๋๋ค. Pet์๋ Dog์ Cat์ด๋ผ๋ 2๊ฐ์ ํ์ ํด๋์ค๊ฐ ์์ต๋๋ค. ์ด์ ๊ฐ๋จํ ๋ฉ์๋๋ฅผ ๋ง๋ค์ด์ผ ํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค iterateAnimals()
. ๋ฉ์๋๋ ๋ชจ๋ ๋๋ฌผ( Animal
, Pet
, Cat
, Dog
) ์ปฌ๋ ์
์ ๊ฐ์ ธ์ ๋ชจ๋ ์์๋ฅผ โโ๋ฐ๋ณตํ๊ณ ๊ฐ ๋ฐ๋ณต ๋์ ์ฝ์์ ๋ฉ์์ง๋ฅผ ํ์ํด์ผ ํฉ๋๋ค. ๋ค์๊ณผ ๊ฐ์ ๋ฐฉ๋ฒ์ ์์ฑํด ๋ด
์๋ค.
public static void iterateAnimals(Collection<Animal> animals) {
for(Animal animal: animals) {
System.out.println("Another iteration in the loop!");
}
}
๋ฌธ์ ๊ฐ ํด๊ฒฐ๋ ๊ฒ ๊ฐ์ต๋๋ค! ๊ทธ๋ฌ๋ ์ต๊ทผ์ ์๊ฒ ๋ ๊ฒ์ฒ๋ผ , List<Cat>
๋ฐ List<Dog>
๋ ! List<Pet>
์ ํ์์ด ์๋๋๋ค . List<Animal>
์ฆ, iterateAnimals()
๊ณ ์์ด ๋ชฉ๋ก์ผ๋ก ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ค๊ณ ํ๋ฉด ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
import java.util.*;
public class Main3 {
public static void iterateAnimals(Collection<Animal> animals) {
for(Animal animal: animals) {
System.out.println("Another iteration in the loop!");
}
}
public static void main(String[] args) {
List<Cat> cats = new ArrayList<>();
cats.add(new Cat());
cats.add(new Cat());
cats.add(new Cat());
cats.add(new Cat());
// Compilation error!
iterateAnimals(cats);
}
}
์ํฉ์ด ์ข์ง ์์ ๋ณด์
๋๋ค! ๊ฐ ์ข
๋ฅ์ ๋๋ฌผ์ ์ด๊ฑฐํ๊ธฐ ์ํด ๋ณ๋์ ๋ฉ์๋๋ฅผ ์์ฑํด์ผ ํฉ๋๊น? ์ค์ ๋ก๋ ๊ทธ๋ ์ง ์์ต๋๋ค. :) ๊ทธ๋ฆฌ๊ณ ๊ณต๊ต๋กญ๊ฒ๋ ์์ผ๋์นด๋๊ฐ ๋์์ด ๋ฉ๋๋ค! ๋ค์ ๊ตฌ์ฑ์ ์ฌ์ฉํ์ฌ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ผ๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค.
public static void iterateAnimals(Collection<? extends Animal> animals) {
for(Animal animal: animals) {
System.out.println("Another iteration in the loop!");
}
}
์ด๊ฒ์ ์์ผ๋์นด๋์
๋๋ค. ๋ณด๋ค ์ ํํ๊ฒ๋ ์ฌ๋ฌ ์ ํ์ ์์ผ๋์นด๋ ์ค ์ฒซ ๋ฒ์งธ ์ ํ์
๋๋ค. ์ด๋ ์ํ ์์ผ๋์นด๋ ๋ก ์๋ ค์ ธ ์์ผ๋ฉฐ ? ๋ก ํํ๋ฉ๋๋ค. ํ์ฅํฉ๋๋ค . ์ด ๊ตฌ์กฐ๋ ์ฐ๋ฆฌ์๊ฒ ๋ฌด์์ ๋งํฉ๋๊น? ์ด๋ ๋ฉ์๋๊ฐ (? extends Animal) ์์ ํ์๋ ๋ชจ๋ ํด๋์ค์ ๊ฐ์ฒด ์ปฌ๋ ์
Animal
๋๋ ๊ฐ์ฒด ์ปฌ๋ ์
์ ํ์ฉํจ์ ์๋ฏธํฉ๋๋ค. Animal
์ฆ, ๋ฉ์๋๋ Animal
, Pet
, Dog
๋๋ Cat
๊ฐ์ฒด์ ์ปฌ๋ ์
์ ํ์ฉํ ์ ์์ต๋๋ค. ์ฐจ์ด๊ฐ ์์ต๋๋ค. ์๋ํ๋์ง ํ์ธํฉ์๋ค.
public static void main(String[] args) {
List<Animal> animals = new ArrayList<>();
animals.add(new Animal());
animals.add(new Animal());
List<Pet> pets = new ArrayList<>();
pets.add(new Pet());
pets.add(new Pet());
List<Cat> cats = new ArrayList<>();
cats.add(new Cat());
cats.add(new Cat());
List<Dog> dogs = new ArrayList<>();
dogs.add(new Dog());
dogs.add(new Dog());
iterateAnimals(animals);
iterateAnimals(pets);
iterateAnimals(cats);
iterateAnimals(dogs);
}
์ฝ์ ์ถ๋ ฅ:
Another iteration in the loop!
Another iteration in the loop!
Another iteration in the loop!
Another iteration in the loop!
Another iteration in the loop!
Another iteration in the loop!
Another iteration in the loop!
Another iteration in the loop!
์ด 4๊ฐ์ ์ปฌ๋ ์
๊ณผ 8๊ฐ์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์๊ณ ์ฝ์์๋ ์ ํํ 8๊ฐ์ ํญ๋ชฉ์ด ์์ต๋๋ค. ๋ชจ๋ ๊ฒ์ด ํ๋ฅญํ๊ฒ ์๋ํฉ๋๋ค! :) ์์ผ๋์นด๋๋ฅผ ์ฌ์ฉํ๋ฉด ํน์ ์ ํ์ ์ฐ๊ฒฐ๋ ํ์ ๋
ผ๋ฆฌ๋ฅผ ๋จ์ผ ๋ฉ์๋์ ์ฝ๊ฒ ๋ง์ถ ์ ์์ต๋๋ค. ๊ฐ ๋๋ฌผ ์ ํ์ ๋ํด ๋ณ๋์ ๋ฉ์๋๋ฅผ ์์ฑํ ํ์๊ฐ ์์ต๋๋ค. ์ฐ๋ฆฌ์ ์ ํ๋ฆฌ์ผ์ด์
์ด ๋๋ฌผ์์ด๋ ์์์ฌ ์ฌ๋ฌด์ค์์ ์ฌ์ฉ๋๋ค๋ฉด ์ผ๋ง๋ ๋ง์ ๋ฉ์๋๊ฐ ํ์ํ์์ง ์์ํด ๋ณด์ธ์ :) ํ์ง๋ง ์ด์ ๋ค๋ฅธ ์ํฉ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค. ์์ ๊ณ์ธต ๊ตฌ์กฐ๋ ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค. ์ต์์ ํด๋์ค๋ ์ด๊ณ Animal
๋ฐ๋ก Pet
์๋์ ํด๋์ค๊ฐ ์๊ณ ๋ค์ ์์ค์ ํด๋์ค๊ฐ ์์ต๋๋ค Cat
. ์ด์ ๊ฐ๋ฅผ ์ ์ธํ ๋ชจ๋ ์ ํ์ ๋๋ฌผ๊ณผ ์๋ํ๋๋ก ๋ฉ์๋๋ฅผ Dog
๋ค์ ์์ฑํด์ผ ํฉ๋๋ค . ์ฆ, ๋ฐ์๋ค์ฌ์ผ ํ๋ค .iterateAnimals()
Collection<Animal>
Collection<Pet>
๋๋ Collection<Car>
์ ํจ๊ป ์๋ํ์ง ์์์ผ ํฉ๋๋ค Collection<Dog>
. ์ด๊ฒ์ ์ด๋ป๊ฒ ๋ฌ์ฑํ ์ ์์ต๋๊น? ๊ฐ ์ ํ์ ๋ํด ๋ณ๋์ ๋ฉ์๋๋ฅผ ์์ฑํ ๊ฐ๋ฅ์ฑ์ ๋ค์ ์ง๋ฉดํ๋ ๊ฒ ๊ฐ์ต๋๋ค./์ฐ๋ฆฌ๊ฐ ์ํ๋ ๊ฒ์ ์ปดํ์ผ๋ฌ์ ์ค๋ช
ํ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ๋ฌด์์
๋๊น? ์ค์ ๋ก ๋งค์ฐ ๊ฐ๋จํฉ๋๋ค! ์ฌ๊ธฐ์ ๋ค์ ํ ๋ฒ ์์ผ๋์นด๋๊ฐ ๋์์ด ๋ฉ๋๋ค. ๊ทธ๋ฌ๋ ์ด๋ฒ์๋ ๋ค๋ฅธ ์ ํ์ ์์ผ๋์นด๋๋ฅผ ์ฌ์ฉํ ๊ฒ์
๋๋ค. ์ฆ, super ๋ฅผ ์ฌ์ฉํ์ฌ ํํ๋๋ ํํ ์์ผ๋์นด๋ ์
๋๋ค .
public static void iterateAnimals(Collection<? super Cat> animals) {
for(int i = 0; i < animals.size(); i++) {
System.out.println("Another iteration in the loop!");
}
}
์ฌ๊ธฐ์ ์๋ฆฌ๋ ๋น์ทํฉ๋๋ค. ๊ตฌ์ฑ ์ ๋ฉ์๋๊ฐ ๊ฐ์ฒด ์ปฌ๋ ์
์ด๋ ํด๋์ค์ ์กฐ์์ ์
๋ ฅ์ผ๋ก ๋ฐ์๋ค์ผ ์ ์์ <? super Cat>
์ ์ปดํ์ผ๋ฌ์ ์๋ฆฝ๋๋ค . ์ด ๊ฒฝ์ฐ ํด๋์ค, ํด๋น ๋ถ๋ชจ ๋ฐ ํด๋น ๋ถ๋ชจ์ ๋ถ๋ชจ ๊ฐ ๋ชจ๋ ์ด ์ค๋ช
๊ณผ ์ผ์นํฉ๋๋ค. ํด๋์ค ๊ฐ ์ฐ๋ฆฌ์ ์ ํ ์ฌํญ๊ณผ ์ผ์นํ์ง ์์ผ๋ฏ๋ก ์ธ์์ ํจ๊ป ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ค๊ณ ํ๋ฉด ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. iterateAnimals()
Cat
Cat
Cat
Pet
Animal
Dog
List<Dog>
public static void main(String[] args) {
List<Animal> animals = new ArrayList<>();
animals.add(new Animal());
animals.add(new Animal());
List<Pet> pets = new ArrayList<>();
pets.add(new Pet());
pets.add(new Pet());
List<Cat> cats = new ArrayList<>();
cats.add(new Cat());
cats.add(new Cat());
List<Dog> dogs = new ArrayList<>();
dogs.add(new Dog());
dogs.add(new Dog());
iterateAnimals(animals);
iterateAnimals(pets);
iterateAnimals(cats);
// Compilation error!
iterateAnimals(dogs);
}
์ฐ๋ฆฌ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ณ ๋ค์ ํ ๋ฒ ์์ผ๋์นด๋๊ฐ ๋งค์ฐ ์ ์ฉํ ๊ฒ์ผ๋ก ํ๋ช
๋์์ต๋๋ค :) ์ด๊ฒ์ผ๋ก ์์
์ด ๋๋ฌ์ต๋๋ค. ์ด์ Java ์ฐ๊ตฌ์์ ์ ๋ค๋ฆญ์ด ์ผ๋ง๋ ์ค์ํ์ง ์ ์ ์์ต๋๋ค. ์ ๋ค๋ฆญ์ ๋ํ 4๊ฐ์ ์ ์ฒด ๊ฐ์๊ฐ ์์ต๋๋ค! ํ์ง๋ง ์ด์ ๋น์ ์ ์ฃผ์ ์ ์ ํตํ๊ณ ๋ฉด์ ์์ ๋น์ ์ ๋ฅ๋ ฅ์ ์ฆ๋ช
ํ ์ ์์ต๋๋ค :) ๊ทธ๋ฆฌ๊ณ ์ด์ ์์
์ผ๋ก ๋์๊ฐ ์๊ฐ์
๋๋ค! ๊ทํ์ ํ์
์์ ์ต๊ณ ์ ์ฑ๊ณต์ ๊ฑฐ๋์ญ์์ค! :)
GO TO FULL VERSION