Logout

Home OOP Option Last Next

D.2.4

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...)

sdfsdfsf

JSR Notes:


Encapsulation Main Advantage - Data Hiding / Security: "Don't mess with me!"

We keep our attributes private - that in a nutshell is encapsulation - so that no other classes of objects can "mess with" our attributes. And remember that there are two ways to "mess with" attributes

1. "Don't mess with my data!" in a traditional data security sense, accessing or changing information.

2. "Don't mess with my code!" in order to prevent data being worked with in a way that is unstable.

The BankAccount class makes for a good, classic example of the traditional security situation: 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) if(codes[i].getPassword.equals(password)){
savingsBalance += amount; } else{ System.out.println("Wrong password for the entered account."); }
}
}
}

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.

 

Further Advantages of Encapsulation's Modularity

Here is the Teaching Note from above, which suggests further advantages of encapsulation:

"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."

This association of Attributes and Methods within one protected class leads also to:

3. Modularity makes for easier debugging and testing, and speedier completion. Further there can be a clearer view/understanding of each section of the problem. (Compare this to a punch-card program!!)

4. Dependencies are limited so the issues of broken connections and waterfall issues are minimized, since data is primarily worked with inside the class where it is defined. (Think of an encapsulated castle.)

Protection / Security Via Data-Hiding leads to:


 

 

 

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.


If you wanted to include all of the above specific advantages into one Mega answer it would 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.