KaiquanMah commited on
Commit
5e6376c
·
verified ·
1 Parent(s): 16c8976

final LocalVariable/ClassAttribute/MethodParam/Method/Class

Browse files
Week 6: Methods of OO Programming/14A. Modifier 'final'+++ ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Finally, this week, let's go through the meaning of the MODIFIER "final" in different contexts.
2
+
3
+ As the name suggests, "final" means that we permanently fix some feature.
4
+ However, it WORKS in slightly DIFFERENT WAYS IN DIFFERENT CONTEXTS.
5
+
6
+
7
+
8
+
9
+ 1
10
+ In the context of a 'LOCAL VARIABLE', the modifier means that the value of the variable CANNOT CHANGE AFTER INITIALISATION.
11
+ This is a way to ensure that the value of the variable does not change even by accident.
12
+
13
+ public static void main(String[] args) {
14
+ // The value of a is always 3
15
+ final int a = 3;
16
+
17
+ // Ollie always points to the same student object
18
+ final Student ollie = new Student("Oliver", "[email protected]", 123);
19
+
20
+
21
+ // HOWEVER, the object CAN CHANGE IF it is NOT IMMUTABLE
22
+ // i.e., if suitable public OPERATIONS are OFFERED TO the CLIENT
23
+ oliver.setStudyCredits(159);
24
+ }
25
+
26
+
27
+
28
+
29
+ 2
30
+ 2A
31
+ In the context of an ATTRIBUTE, the operation is similar.
32
+ The difference is that the value can either be given immediately at the time of definition…
33
+
34
+ class Ball {
35
+ // SET VALUE AT ATTRIBUTE LEVEL
36
+ private final double diameter = 10.0;
37
+
38
+ }
39
+
40
+ 2B
41
+ … or leave the value to be given in the CONSTRUCTORS.
42
+ If the value is not given at the time of definition, it must be given in each constructor.
43
+ After this, the value cannot change.
44
+
45
+ class Ball {
46
+ private final double diameter;
47
+
48
+ public Ball(double diameter) {
49
+ // SET VALUE IN THE CONSTRUCTOR
50
+ this.diameter = diameter;
51
+ }
52
+ }
53
+
54
+
55
+
56
+
57
+
58
+
59
+ 3
60
+ A PARAMETER can also be defined with the 'final' modifier.
61
+ In this case, the value of the parameter variable cannot change.
62
+ For example, objects passed as parameters could be equipped with a final modifier,
63
+ so that the original reference is not accidentally lost within the method.
64
+
65
+ public static void removeNegatives(final ArrayList<Integer> list) {
66
+ // Now the value of the parameter variable list cannot change,
67
+ // i.e., it always points to the list that was passed
68
+ // as a parameter.
69
+
70
+
71
+
72
+
73
+ 4
74
+ In the context of METHODS and CLASSES, the final modifier is related to INHERITANCE.
75
+ A method equipped with the modifier cannot be overridden in a derived class:
76
+
77
+ class SecretAgent {
78
+ private String name;
79
+ private String code;
80
+
81
+ public SecretAgent(String name, String code) {
82
+ this.name = name;
83
+ this.setCode(code);
84
+ }
85
+
86
+
87
+ // 'final' METHOD
88
+ public final void setCode(String code) {
89
+ // The class can be inherited, but this method cannot
90
+ // be overridden in the derived class
91
+ if (SecretAgent.codeOk(code)) {
92
+ this.code = code;
93
+ }
94
+ else {
95
+ this.code = "000";
96
+ }
97
+ }
98
+
99
+ public static boolean codeOk(String code) {
100
+ if (code.length() != 3 || !code.startsWith("00")) {
101
+ return false;
102
+ }
103
+ try {
104
+ Integer.valueOf("" + code.charAt(2));
105
+ return true;
106
+ } catch (NumberFormatException e) {
107
+ return false;
108
+ }
109
+ }
110
+ }
111
+
112
+
113
+ A good example of this could be a setting method that is called in the constructor
114
+ - if it is overridden in the derived class, the operation of the class can change significantly.
115
+
116
+ If a class is equipped with the 'final' modifier, the 'final' CLASS CANNOT BE INHERITED at all by other classes.
117
+ A typical example is the Java 'String' class, whose inheritance attempt leads to a compiler error message:
118
+
119
+
120
+ eg1
121
+ // Now Ball cannot be inherited, an inheritance attempt gives an error
122
+ final class Ball {
123
+ private final double diameter;
124
+
125
+ public Ball(double diameter) {
126
+ this.diameter = diameter;
127
+ }
128
+ }
129
+
130
+
131
+ class SoccerBall extends Ball { } // Compiler error!
132
+
133
+
134
+
135
+
136
+
137
+
138
+ eg2
139
+ // String is declared as:
140
+ public final class String {
141
+ // ...
142
+ }
143
+
144
+
145
+ class MyString extends String { } // COMPILER ERROR!
146
+
147
+
148
+
149
+
150
+ ========================================
151
+
152
+
153
+
154
+
155
+
156
+ The Last Piece on the Board
157
+ At the end of chapter 6, let's implement a simple game.
158
+ The game starts with an 'empty game board', where each player alternately places 1-3 of their own game pieces according to their own choice.
159
+ The player who places the last piece on the board wins.
160
+
161
+ A piece can only be placed in an empty square.
162
+ There can be different amounts of pieces on the board at different rounds of the game.
163
+
164
+
165
+