Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132930 views
License: OTHER
1
public static class Animal {
2
public String toString() {
3
return getClass().getSimpleName();
4
}
5
}
6
public static class Rat extends Animal {}
7
public static class Lion extends Animal {}
8
public static class Cage<T extends Animal> {
9
/* above */
10
}
11
12
public static void main(String[] args) {
13
Cage<Animal> animals = new Cage<Animal>();
14
Cage<Lion> lions = new Cage<Lion>();
15
16
// OK to put a Rat into a Cage<Animal>
17
animals.add(new Rat());
18
19
lions.add(new Lion());
20
21
// invoke the super generic method
22
lions.transferTo(animals);
23
animals.showAnimals();
24
}
25
26