• 코드:
​x
 
1
import java.util.*;
2
​
3
class LandAnimal { public void crying() { System.out.println("육지동물"); } }
4
class Cat extends LandAnimal { public void crying() { System.out.println("냐옹냐옹"); } }
5
class Dog extends LandAnimal { public void crying() { System.out.println("멍멍"); } }
6
class Sparrow { public void crying() { System.out.println("짹짹"); } } 
7
​
8
class AnimalList<T> {
9
    ArrayList<T> al = new ArrayList<T>();
10
    
11
    public static void cryingAnimalList(AnimalList<? extends LandAnimal> al) {
12
        LandAnimal la = al.get(0);
13
        la.crying();
14
    }
15
    
16
    void add(T animal) { al.add(animal); }
17
    T get(int index) { return al.get(index); }
18
    boolean remove(T animal) { return al.remove(animal); }
19
    int size() { return al.size(); }
20
}
21
​
22
public class prog {
23
    public static void main(String[] args) {
24
        AnimalList<Cat> catList = new AnimalList<Cat>();
25
        catList.add(new Cat());
26
        AnimalList<Dog> dogList = new AnimalList<Dog>();
27
        dogList.add(new Dog());
28
        
29
        AnimalList.cryingAnimalList(catList);
30
        AnimalList.cryingAnimalList(dogList);
31
    }
32
}
표준입력 & 실행옵션