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