Information hiding is the principle of allowing access to parts of an object that are meant to be accessed by other programmers while protecting the inner workings of the object from abuse, unwanted changes or incorrect use. This can be don using access modifiers in conjunction with special methods called accessors and mutators.
Access modifiers are keywords in a programming language which define how a particular class entity (method or attribute) can be accessed.
In Java there are four modifiers:[1]
No modifier - If no modifier is specified then Java will allow any entity in the same class or package access to the entity.
The "public" modifier indicates that any other entity in any other class can have access
The "private" access modifier indicates that only other entities in the same class have access.
The "protected" access modifier means that any entity in the same class, any subclass or any class in the same package can have access.
It is considered good programming practice to use the most restrictive access modifier possible. Then if access is required, create accessor or mutator methods.
An accessor, also known as a getter, is a piece of code which returns the value of a field or attribute or the state of the object. The datatype of value returned is almost always the same as the data type of the attribute whose value is being returned. Accessors usually have a less restrictive access modifier than their underlying attributes.
public class Item {
private double costPrice = 14.00;
private double markUp = 1.07;
private boolean onSale = false;
//Accessor example A:
public double getCostPrice() {
return costPrice;
}
//Accessor example B:
public boolean isOnSale() {
return onSale;
}
//Accessor example C:
public double getSellPrice () {
return costPrice * markUp;
}
}
In example A the accessor simply returns the value of the costPrice attribute. The private modifier prevents any entity not in the same class from changing the value of or even reading the costPrice attribute. Adding a accessor allows entities to get the value of the costPrice attribute - effectively making the field read-only.
In example C the accessor method returns a value calculated by multiplying the costPrice and the markUp attributes.
The naming convention for accessor methods is to use "get" followed by the name of the underlying attribute. Accessors which return a boolean value should be named "is" followed by the name of the attribute (as shown in example B).
A mutator, also known as a setter, is a method which changes the state of that object. Put another way, a mutator is a piece of code which will usually result in a change of one or more of that objects attributes. Mutator methods usually, but not always, have no return value.
public class Auction {
private double currentBid = 1.00;
//Mutator example A:
private void setCurrentBid(double newBid) {
if(newBid > currentBid) {
currentBid = newBid
}
}
}
In example A, the mutator method allows the current bid on an auction to be raised.
[1] Oracle. Controlling Access to Members of a Class. (n.d.) Available at: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html (Accessed 31 July 2012)