Explain the advantages of encapsulation.


Teaching Note:

For example, the scope of data should be confined to the object in which it is defined as far as possible in order to limit side effects and dependencies.


Sample Question:


Q1. By making reference to any of the above classes, describe two benefits that a programming team would gain from the use of encapsulation.


Q2. Outline two benefits provided by encapsulation.


Q3. When creating objects, encapsulation is an important design consideration. Outline, with direct reference to the Xyz class, how security can be enhanced by using encapsulation.


Q4. In relation to the Abc class, outline one advantage of encapsulation.


Q5. Outline two advantages that the programming team should expect from using an OOP approach.


(By the way, in case you missed it, 5 questions from five years of past papers, all asking the same thing. Hint, hint...)


JSR Notes:

We keep our attributes private - that in a nutshell is encapsulation - so that no other classes of objects can "mess with" our attributes. The BankAccount class makes for a good, classic example: only the BankAccount instance should be able to work directly with the attributes savingsBalance, or checkingBalance. Otherwise, someone else could program something to directly access those attributes and change them.

class BankAccount{

private double savingsBalance = 100;
public void adjustSavingsBalance(Code authenticationCode, String password, double amount){
for(int i = 0; i < codes.length; i++){
if(codes[i].getAuthenticationCode == authenticationCode && codes[i].getPassword.equals(password)){
savingsBalance += amount;

A person wishing to use a certain BankAccount instance would go, from another class:

Code myAuthenticationCode = new authenticationCode("John Rayworth", 123412134); 
bankAccountInstnace.adjustSavingsBalance(myAuthenticationCode, good@#$pass987word, -10);

But if they tried to go:

bankAccountInstnace.savingsBalance = -1000000;

It would not work, because savingsBalance cannot be directly accessed outside of the BankAccount class.

Without encapsulation - i.e. if savingsBalance was either "public", or just non-private double savingBalance, the above line would work! And anyone with access to the code could drain or add to anyone's bank account.


Taking the above real OOP example, and looking at the Teaching Note:

"For example, the scope of data should be confined to the object in which it is defined as far as possible in order to limit side effects and dependencies."

The idea is that the "scope" of the data savingsBalance is indeed confined to its instance of the BankAccount class, and this limits it being used directly. The side effect that is limited is that savingsBalance cannot be directly manipulated - and money drained from an account! - rather savingsBalance is only able to be accessed indirectly in a controlled way by the pubic method.


In Summary, Back to the Assessment Statement:
Explain the advantages of encapsulation.

The advantages are actually many, but a couple of key things lead to other advantages, and there's one overall benefit. So:


By placing all attributes and methods that relate to a particular object/entity together, data is protected; the data can only be manipulated in ways dictated by the methods that work with it. This results in improved stability and reliability of programs.


Association of Attributes and Methods within one class leads to:

1. Clearer view/understanding of each section of the problem

2. Limit Dependencies so the issues of broken connections and waterfall issues are minimized

Protection / Security Via Data-Hiding leads to:

3. Limited Side-effects: Private encapsulation protects the values of the data stored within the object from accidental changes made by other classes.

4. Modularity and this makes for easier debugging and testing, and speedier completion (refer to D.2.10)

And all of this leads to:

5. Improved stability and reliability of programs.



If you wanted to put this all together in one Mega answer it should look something like this:

Encapsulation brings the primary advantage of stability and reliability in programs. This is done via data protection, in that data attributes, made private, can only be accessed and manipulate in specific ways controlled by their public methods. And the general grouping of attributes and their related methods adds to clarity, resulting in easier understanding and less errors. Modularity of design, via encapsulation, also adds to the ease of maintenance, testing, and speed of development.