forked from dr-cs/intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListTypesDemo.java
More file actions
33 lines (25 loc) · 916 Bytes
/
ArrayListTypesDemo.java
File metadata and controls
33 lines (25 loc) · 916 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
import java.util.ArrayList;
public class ArrayListTypesDemo {
static abstract class Gnome {
public String name;
public Gnome(String name) {
this.name = name;
}
}
static class GardenGnome extends Gnome {
public GardenGnome(String name) { super(name); }
}
static class UnderwearGnome extends Gnome {
public UnderwearGnome(String name) { super(name); }
}
public static void main(String[] args) {
ArrayList<Gnome> gnomes = new ArrayList<>();
ArrayList<GardenGnome> gardenGnomes = new ArrayList<>();
ArrayList<UnderwearGnome> underwearGnomes = new ArrayList<>();
// Fine: Gnome is a superclass
gnomes.add(new GardenGnome("Corny"));
gnomes.add(new UnderwearGnome("Jaques"));
// Nuh, uh! Type incompatibility.
underwearGnomes.add(new GardenGnome("Carrot Top"));
}
}