-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathGenericBoundedEx.java
More file actions
73 lines (48 loc) · 979 Bytes
/
GenericBoundedEx.java
File metadata and controls
73 lines (48 loc) · 979 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
interface Item {
String info();
}
interface Plant {
String getColor();
}
class Bike implements Item {
@Override
public String info() {
return "This is a bike";
}
}
class Chair implements Item {
@Override
public String info() {
return "This is a chair";
}
}
class Flower implements Item, Plant {
private String color;
public Flower(String color) {
this.color = color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String getColor() {
return this.color;
}
@Override
public String info() {
return String.format("This is %s flower", this.color);
}
}
// Generic bounded example
void main() {
Chair chair = new Chair();
doInform2(chair);
Flower flower = new Flower("red");
doInform(flower);
}
<T extends Item & Plant> void doInform(T item) {
System.out.println(item.info());
}
<T extends Item> void doInform2(T item) {
System.out.println(item.info());
}