-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCovariantArrays.java
More file actions
45 lines (33 loc) · 923 Bytes
/
CovariantArrays.java
File metadata and controls
45 lines (33 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package generics;
import java.util.*;
import java.lang.reflect.*;
/**
* RUN:
* javac generics/CovariantArrays.java && java generics.CovariantArrays
* OUTPUT:
* java.lang.ArrayStoreException: generics.Fruit
* java.lang.ArrayStoreException: generics.Orange
*/
public class CovariantArrays {
public static void main(String[] args) {
Fruit[] fruit = new Apple[10];
fruit[0] = new Apple(); // OK
fruit[1] = new Jonathan(); // OK
try {
fruit[0] = new Fruit(); // ArrayStoreException
}
catch(Exception e) {
System.out.println(e);
}
try {
fruit[0] = new Orange(); // ArrayStoreException
}
catch(Exception e) {
System.out.println(e);
}
}
}
class Fruit {}
class Apple extends Fruit {}
class Jonathan extends Apple {}
class Orange extends Fruit {}