-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAsListInterface.java
More file actions
40 lines (32 loc) · 1.14 KB
/
AsListInterface.java
File metadata and controls
40 lines (32 loc) · 1.14 KB
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
package holding;
/**
* RUN:
* javac holding/AsListInterface.java && java holding.AsListInterface
* OUTPUT:
* [holding.Crusty@9931f5, holding.Slush@19ee1ac, holding.Powder@1f1fba0]
* [holding.Light@1befab0, holding.Heavy@13c5982]
* [holding.Light@1186fab, holding.Heavy@14b7453]
*/
import java.util.*;
public class AsListInterface {
public static void main(String[] args) {
List<Snow> snow1 = Arrays.asList(new Crusty(), new Slush(), new Powder() );
System.out.println(snow1);
// error: incompatible types
//
// required: List<Snow>
// found: List<Powder>
// List<Snow> snow2 = Arrays.asList(new Light(), new Heavy());
List<Snow> snow3 = new ArrayList<Snow>();
Collections.addAll(snow3, new Light(), new Heavy());
System.out.println(snow3);
List<Snow> snow4 = Arrays.<Snow>asList(new Light(), new Heavy());
System.out.println(snow4);
}
}
class Snow { }
class Powder extends Snow { }
class Light extends Powder { }
class Heavy extends Powder { }
class Crusty extends Snow { }
class Slush extends Snow { }