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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Encapsulation and internal integrity
|
| 2 |
+
Before going into more detail about methods, let's look at one of the basic principles of object-oriented programming, namely encapsulation.
|
| 3 |
+
The idea of encapsulation is to hide INFORMATION from the client that the CLIENT DOES NOT NEED to ACCESS directly.
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
Why is encapsulation important?
|
| 8 |
+
|
| 9 |
+
Let's go back to the earlier Student example:
|
| 10 |
+
class Student {
|
| 11 |
+
String name;
|
| 12 |
+
String studentId;
|
| 13 |
+
int studyPoints;
|
| 14 |
+
|
| 15 |
+
public Student(String name, String studentId, int studyPoints) {
|
| 16 |
+
this.name = name;
|
| 17 |
+
this.studentId = studentId;
|
| 18 |
+
this.studyPoints = studyPoints;
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
When attribute values are not protected, the client is free to use them via an object reference, e.g.
|
| 24 |
+
public class Testclass {
|
| 25 |
+
public static void main(String[] args) {
|
| 26 |
+
Student sam = new Student("Sam Student", "12345", 45);
|
| 27 |
+
|
| 28 |
+
System.out.println("Student's name: " + sam.name);
|
| 29 |
+
System.out.println("Student id: " + sam.studentId);
|
| 30 |
+
System.out.println("Study points: " + sam.studyPoints);
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
Program outputs:
|
| 34 |
+
Student's name: Sam Student
|
| 35 |
+
Student id: 12345
|
| 36 |
+
Study points: 45
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
VS
|
| 40 |
+
|
| 41 |
+
However, a client may also (often ACCIDENTALLY) ASSIGN values to attributes that are not allowed for the
|
| 42 |
+
meaning and rational operation of the object:
|
| 43 |
+
|
| 44 |
+
Student sam = new Student("Sam Student", "12345", 45);
|
| 45 |
+
sam.studyPoints -= 50;
|
| 46 |
+
System.out.println(sam.studyPoints);
|
| 47 |
+
|
| 48 |
+
Program outputs:
|
| 49 |
+
-5
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
When an attribute of a object takes on an "illegal" value, the internal integrity of the object is broken.
|
| 55 |
+
Thus, INTERNAL INTEGRITY means that the object is in such a state that all its attributes have an ALLOWED VALUE.
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
=================
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
Observation methods
|
| 66 |
+
|
| 67 |
+
Features can be hidden from the customer by making them private.
|
| 68 |
+
This is done by adding the attribute 'private' to the feature definition.
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
So let's make the Student attributes private by adding a 'private' attribute to their attributes:
|
| 72 |
+
|
| 73 |
+
class Student {
|
| 74 |
+
private String name;
|
| 75 |
+
private String studentId;
|
| 76 |
+
private int studyPoints;
|
| 77 |
+
|
| 78 |
+
// The constructor is of course, public
|
| 79 |
+
public Student(String name, String studentId, int studyPoints) {
|
| 80 |
+
this.name = name;
|
| 81 |
+
this.studentId = studentId;
|
| 82 |
+
this.studyPoints = studyPoints;
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
Now the client's attempt to use attributes is stopped by a TRANSLATION ERROR
|
| 88 |
+
public class TestClass {
|
| 89 |
+
public static void main(String[] args) {
|
| 90 |
+
Student sam = new Student("Sam Student", "12345", 45);
|
| 91 |
+
|
| 92 |
+
sam.studyPoints -= 50;
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
Program throws an error:
|
| 96 |
+
The field Student.studyPoints is not visible
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
It is good practice to always define attributes as 'private'.
|
| 103 |
+
But how can the client now access the object's data content?
|
| 104 |
+
|
| 105 |
+
For this purpose, methods are written in the class that allow the client to observe and
|
| 106 |
+
(if necessary) modify the data content.
|
| 107 |
+
Let's start by defining an OBSERVATION METHOD for credits in the class:
|
| 108 |
+
|
| 109 |
+
class Student {
|
| 110 |
+
private String name;
|
| 111 |
+
private String studentId;
|
| 112 |
+
private int studyPoints;
|
| 113 |
+
|
| 114 |
+
public Student(String name, String studentId, int studyPoints) {
|
| 115 |
+
this.name = name;
|
| 116 |
+
this.studentId = studentId;
|
| 117 |
+
this.studyPoints = studyPoints;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
// OBSERVATION METHOD for the study points
|
| 121 |
+
public int getStudyPoints() {
|
| 122 |
+
return this.studyPoints;
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
The name of the method can be chosen freely; the course uses the getAttributeName() naming convention,
|
| 128 |
+
which seems a bit funny in English-Mumbojumbo.
|
| 129 |
+
This is because naming an observation method in this way is common practice in Java,
|
| 130 |
+
and for example most Java editors use this format by default.
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
Now the client can return the number of credits by calling the method:
|
| 135 |
+
public class TestClass {
|
| 136 |
+
public static void main(String[] args) {
|
| 137 |
+
Student sam = new Student("Sam Student", "12345", 45);
|
| 138 |
+
|
| 139 |
+
System.out.println("Study points: " + sam.getStudyPoints());
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
Program outputs:
|
| 143 |
+
Study points: 45
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
Let's add other observation methods to the category:
|
| 152 |
+
|
| 153 |
+
class Student {
|
| 154 |
+
private String name;
|
| 155 |
+
private String studentId;
|
| 156 |
+
private int studyPoints;
|
| 157 |
+
|
| 158 |
+
public Student(String name, String studentId, int studyPoints) {
|
| 159 |
+
this.name = name;
|
| 160 |
+
this.studentId = studentId;
|
| 161 |
+
this.studyPoints = studyPoints;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
// OBSERVATION METHODs for all attributes
|
| 165 |
+
|
| 166 |
+
public String getName() {
|
| 167 |
+
return name;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
public int getStudyPoints() {
|
| 171 |
+
return studyPoints;
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
public String getStudentId() {
|
| 175 |
+
return studentId;
|
| 176 |
+
}
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
Example on method calls:
|
| 183 |
+
public class TestClass {
|
| 184 |
+
public static void main(String[] args) {
|
| 185 |
+
Student sam = new Student("Sam Student", "12345", 45);
|
| 186 |
+
|
| 187 |
+
System.out.println("Student: " + sam.getName());
|
| 188 |
+
System.out.println("Study points: " + sam.getStudyPoints());
|
| 189 |
+
System.out.println("Student id: " + sam.getStudentId());
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
Program outputs:
|
| 193 |
+
Student: Sam Student
|
| 194 |
+
Study points: 45
|
| 195 |
+
Student id: 12345
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
When we compare the observation method with the methods we wrote earlier in the course,
|
| 200 |
+
we notice one crucial difference:
|
| 201 |
+
the METHOD that target an OBJECT DO NOT USE the attribute 'static'.
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
|