forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericWildcardEx.java
More file actions
50 lines (35 loc) · 895 Bytes
/
GenericWildcardEx.java
File metadata and controls
50 lines (35 loc) · 895 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
45
46
47
48
49
50
package com.zetcode;
import java.util.ArrayList;
import java.util.List;
abstract class Shape {
abstract void draw();
}
class Rectangle extends Shape {
@Override
void draw() {
System.out.println("Drawing rectangle");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing circle");
}
}
// Generic wildcard example
public class GenericWildcardEx {
public static void main(String[] args) {
List<Rectangle> list1 = new ArrayList<>();
list1.add(new Rectangle());
List<Circle> list2 = new ArrayList<>();
list2.add(new Circle());
list2.add(new Circle());
drawShapes(list1);
drawShapes(list2);
}
private static void drawShapes(List<? extends Shape> lists) {
for (Shape s : lists) {
s.draw();
}
}
}