What will you build

You will build a small application that will use the concept of encapsulation. You will see what is accessors or mutators

What You'll Learn

What will you need

You can find principles of encapsulation in everyday life, it is also called a black box. For example, one of the public interface of a car is the ignition switch (to start the car), but you don't know what is happening under the hood when you are turning the key or pushing the button to start the car. The "implementation" of the ignition switch is not necessarily secret, but the driver doens't need to know how the ignition is working, to use the car. The "implementation" is encapsulate for the driver.

In programming, you have the same principle, which is call encapsulation. The developper of a class will provide the bundling of data and methods in the same "capsule" for the user-developper to use. The methods that are related to the data in a capsule shouldn't be separated but should be associated to the date in the same "capsule". In OOP this capsule will be called class.

The user-developper of the capsule/class will have access to the public details of the class in order to use this class. It is often that the methods it is only the methods that are public and the data are hiden, however, even if related to the concept of encapsulation, this is data hiding, not encapsulation principle.

The reasons of using encapsulation are multiple:

Information hiding is a concept very close to encapsulation and very often confused with encapsulation, but we can have encapsulation without information hiding.

In Java, we are using the access modifier to hide the information and it is a rule to have (most of the time) the instances variables private and the methods (accessor-mutator or getter-setter) that have access to these data public. Other methods could be either public, private or defined with other access specifier.

The advantages of using information hiding are numerous:

To simplify the rules for the moment, and to follow the good practices, we can determine that the access specifier of the instance variables are always private, and the methods (accessor-mutator or getter-setter) that have access to these data are public.

For example, a class Tea.

The instances variables are set as private.

public class Tea{

/** 
The instance variable access modifier of the Tea class are set to private; 
only this class have access to it
*/

private String typeTea;
private double price;

public Tea(){
	typeTea = "darjeeling";
	price = 0.10;
}

public Tea(String typeTea){
	this.typeTea = typeTea;
}

The getter/accessor is a method that access an object and return some information about it without changing the object. By convention, the getter method start with the word get and finish with the name of the variable that it will return. The getter needs to be public, if the developper wants to this method to be accessible outside of the class.

//the getter of the price
public double getPrice(){
	return price;
}

//the getter of the type of tea
public String getTypeTea(){
	return typeTea;
}

The setter/mutator is a method that will change the data and not returning anything. By convention, the setter method start with the word set and finish with the name of the variable that it will change. The getter needs to be public, if the developper wants to this method to be accessible outside of the class.

//the setter of the price
public void setPrice(double price){
 	this.price = price;
}

}

As usual a small class to test the class Tea.

public class TestTea{

public static void main(String[]args){

	Tea darjeeling = new Tea();
	System.out.println(darjeeling.getTypeTea());
	System.out.println(darjeeling.getPrice());
	darjeeling.setPrice(10.0);
	System.out.println(darjeeling.getPrice());
	
	
	
	Tea english = new Tea("English Breakfast");
	System.out.println(english.getTypeTea());
	System.out.println(english.getPrice());
	english.setPrice(12.0);
	System.out.println(english.getPrice());
	}
}

In the following example, I will demonstrate why it is important to have the instance variable access set as private. I will implement a class BankAccount where the instance variable access modifier is set as public, which should not be the case.

When we normally think about a programm that will deal with a Bank account, we imagine that it is not possible to change the balance of a bank account without the user either to deposit or withdraw some money.

By consequence, I will also implement two methods, one that will subtract the withdraw from the balance in the bank account, and one that will add the amount to the balance.

The bank account class:

public class BankAccount{

/** 
The instance variable of the bank account are private, 
only this class have access to it
*/

public double balance;

/** 
The constructor of the bankAccount class is public, 
if not it will not be possible to instantialise an 
object Bank outside of the class BankAccount.
*/

public BankAccount() {
	balance = 0;
}


public void deposit(double amount) {
	balance = balance + amount;
}

public void withdraw(double amount) {
	balance = balance - amount;
}

public double getBalance(){
	return balance;
}

}

The usual test class

public class TestBankAccount{

public static void main(String[]args){

	BankAccount account = new BankAccount();
	System.out.println(account.getBalance());
	account.deposit(10);
	System.out.println(account.getBalance());
	account.withdraw(5.0);
	System.out.println("should have 5 in the bank account : "+account.getBalance());
	
	account.balance = 10;
	System.out.println("should have 5 in the bank account : "+account.getBalance());
	}
}

As you can notice, the class TestBankAccount can have access to the value of balance directly without using the withdraw or deposit methods and changing the value of the balance. In real life, this will be really catastrophic, as it could be possible to change the balance of a real bank account without the withdraw or deposit method.

What should be change in this class?

The Abstraction, Encapsulation, and Information Hiding, Edward V. Berard [Wirfs-Brock et al, 1990] R. Wirfs-Brock, B. Wilkerson, and L. Wiener, Designing Object-Oriented Software, Prentice-Hall, Englewood Cliffs, New Jersey, 1990.

[Budd, 1991]. T. Budd, An Introduction to Object-Oriented Programming, Addison-Wesley, Reading, Massachusetts, 1991.

You can find the code for this example in https://git.cardiff.ac.uk/ASE_GROUP_2020/code_for_codelabs.git