public class Person {
♦ Field firstName of the class is encapsulated because is declared private. private String firstName; // encapsulated field public String getFirstName() {
return firstName;
}
public void setFirstName (String newName) {
firstName = newName;
}
}♦ No one, outside the class, can directly access this field, except through certain public methods and only if they exist. ♦ Hence, encapsulation is the technique of making the fields in a class private and providing access to them via public methods. ♦ Even within the class itself, which has direct access to private fields anyway, it is a good practise to access them through these public methods. Then we have perfect encapsulation! |
♦ The methods that allow a field (eg. firstName) to be viewed are known as accessor or getter (eg. getFirstName). public String getFirstName() { // accessor or getter
return firstName;
}
♦ Their name starts with get followed by the field name with capital letter (implied agreement of the Java community). |
♦ The methods that allow a field to be changed are known as mutator or setter (eg. setFirstName). public void setFirstName (String newName) { // mutator or setter
firstName = newName;
}
♦ Their name starts with set followed by the field name with capital letter (implied agreement of the Java community). |
♦ However, there is also another convention of naming these methods. Just using the name of the field.
public String firstName() { // accessor or getter
return firstName;
Thanks to Java's overloading future there is no confusion between the firstName() and firstName("John")} public void firstName (String newName) { // mutator or setter
firstName = newName;
}methods as well as with the field name itself firstName |
♦ To see the usefulness of encapsulation, suppose that firstName was public and you were registering the names e.g. John, Mary, etc. Other classes could access firstName directly (John, Mary etc). So far so good. public String firstName; // non encapsulated field
♦ Imagine now, that the client changes his mind and wants the names to appear with capital letters (JOHN, MARY etc).What would you do? ♦ 1st solution is to change all the names to capital letters. Big trouble! ♦ 2nd solution, inform all involved classes to change their code and capitalize the name themselves. Even more trouble! ♦ This nightmare will be stopped by cutting direct access to firstName, making it private. A public accessor method (eg getFirstName) will return the name capitalized in whatever class it is interested in. private String firstName; // encapsulated field public String getFirstName() { // accessor or getter
return firstName.toUpperCase(); // John → JOHN
}No names will be changed and no other classes will be disturbed. |
♦ An encapsulated field, whose value can only be read, is called read-only:
private String firstName; // read-only public String firstName() { // accessor or getter
return firstName;
} |
♦ An encapsulated field, whose value can only be set, is called write-only:
private String firstName; // write-only public void firstName (String newName) { // mutator or setter
firstName = newName;
} |