KaiquanMah commited on
Commit
3e1b953
·
verified ·
1 Parent(s): ce3d78b

class PersonContainer<T extends Person>

Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/07A. Generics and Class Hierarchies ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The use of 'generic type definitions' in classes naturally LIMITS the FUNCTIONALITY OF METHODS that use type definitions.
2
+ Since the 'generic type' can be anything when an object is created from a class,
3
+ we can't use through a variable of the type 'anything other than the features defined in the Object class'.
4
+ =????????
5
+
6
+ => because Java doesn't know what T actually is at compile time,
7
+ => you can't assume specific behavior (like calling .length() or + operations) unless explicitly told.
8
+ VS
9
+ => In Java, all classes inherit from 'Object'.
10
+ => So if you have a generic type T, Java treats it as if it were an 'Object'
11
+ => — meaning you can only safely call methods defined in 'Object', such as:
12
+ .toString()
13
+ .equals()
14
+ .hashCode()
15
+ => You cannot assume anything more specific (like .compareTo(), .length(), +, etc.) unless you bound the generic type to a dtype u want.
16
+
17
+
18
+
19
+
20
+ The following code, for example, would give a compilation error:
21
+
22
+
23
+ class Box<TYPE> {
24
+ private TYPE value;
25
+
26
+ public Box(TYPE value) {
27
+ this.value = value;
28
+ }
29
+
30
+ public TYPE getValue() {
31
+ return value;
32
+ }
33
+
34
+ public void setValue(TYPE value) {
35
+ this.value = value;
36
+ }
37
+
38
+ public void isValueLongerThanZero() {
39
+ // This gives a compilation error, because the method
40
+ // length() is not in all classes
41
+ if (value.length() > 0) {
42
+ System.out.println("Longer than 0!");
43
+ }
44
+ }
45
+ }
46
+
47
+
48
+ THE CODE GIVES A COMPILATION ERROR:
49
+ The method length() is undefined for the type TYPE
50
+
51
+
52
+
53
+ ==================================================================================
54
+
55
+
56
+
57
+
58
+
59
+ However, we can LIMIT the DEFINITION of the generic type by using the 'extends' keyword.
60
+
61
+ For example, the following 'generic definition' indicates that the 'implementing type' must inherit the class 'Person':
62
+
63
+ class PersonContainer<T extends Person> {
64
+ private T person;
65
+
66
+ public PersonContainer(T person) {
67
+ this.person = person;
68
+ }
69
+
70
+ public String giveName() {
71
+ // Now this works, because T inherits 'Person'
72
+ return person.getName();
73
+ }
74
+
75
+ public String giveEmail() {
76
+ return person.getEmail();
77
+ }
78
+ }
79
+
80
+
81
+
82
+
83
+
84
+
85
+ The type to be inherited can also be an 'interface', even though the keyword 'extends' is still used, confusingly.
86
+
87
+ For example, types implementing the 'Comparable interface '
88
+ naturally have the method compareTo in use.
89
+ Note that the generic definition T is also repeated in the name of the interface:
90
+
91
+ class Comparator<T extends Comparable<T>> {
92
+ public Comparator() {}
93
+
94
+ public T giveGreater(T element1, T element2) {
95
+ if (element1.compareTo(element2) < 0) {
96
+ return element2;
97
+ }
98
+ else {
99
+ return element1;
100
+ }
101
+ }
102
+ }
103
+
104
+
105
+
106
+
107
+
108
+
109
+ ==================================================================================
110
+
111
+
112
+ Generics also involves other issues not covered in this section,
113
+ such as statically, generically typed methods or wildcard characters.
114
+ You can read more about these in Oracle's tutorial:
115
+ https://docs.oracle.com/javase/tutorial/java/generics/index.html
116
+
117
+