When anyone can change anything
After this: Say what goes wrong with an open field, and what encapsulation means.
On this page · 5 sections · 3 checks
A bank account has a rule that everyone knows: you cannot take out more money than is in it. In the Classes and objects tutorial, withdraw kept that rule by checking the amount first. But the balance field itself was open. Any line of the program could write a new number into it, without calling withdraw at all. This lesson shows what goes wrong when that happens. The next three lessons show how a class can stop it.
A field anyone can change
Here is the first Account class from Classes and objects, the version with its two fields, owner and balance, and no methods yet. Code outside the class can read a field with a dot, as in a.balance, and it can change the field the same way.
Follow the program by hand:
- It makes Ana's account with a balance of 100.
- A line outside the class takes 500 from the balance: 100 minus 500 is -400. The line stores -400 in the field.
- It prints the balance:
-400.
Nothing checked the change. The class has no line that runs when its field is written, so it never had a chance to refuse. The rule "never below zero" lived in the programmer's head, not in the code.
Run it
Python runs in the browser when you press Run on the block below. Java does not run here: if you are learning Java, read the Java version in the block, and paste it into a Java tool on your computer when you want to run it for real.
Change 500 to 50 and run again: the balance is 50. Now change it to 5000: the program prints -4900 without any complaint. The class cannot tell a sensible change from a wrong one, because it is never asked.
One wrong number, many suspects
A real banking program does not change the balance in one place. It might do it in twenty: a function for rent, one for refunds, one for fees, spread over several files. Suppose one morning a customer's balance shows -400. Which line did it? Any of the twenty could have. To find out, you have to read them all. And next month someone writes a twenty-first.
Now picture the other way round:
- The balance can change only through the class's own methods, such as
depositandwithdraw. - Each of those methods checks the change before it makes it, and refuses a change that breaks the rule.
- Code outside the class asks for a change by calling a method. It cannot write the field directly.
Then the rule is written once, inside the class. Every change passes through it, including the twenty-first. And a wrong balance can only come from a few lines inside the class, so that is where you look.
The idea, and its name
The object keeps its data to itself, and the only way to change that data is through the object's own methods, which check every change. This idea is called . The word comes from a capsule: the data sits inside, and the methods are the only way in.
| Open field | Guarded data | |
|---|---|---|
| Who can change the balance | any line of the program | only the class's own methods |
| Where the rule is written | wherever someone remembered it | once, in the class |
| Where to look for a wrong balance | everywhere | inside the class |
The rest of this tutorial builds the right-hand column, one piece at a time. Lesson 2 closes the field to outside code. Lesson 3 lets outside code read the balance without being able to write it. Lesson 4 writes the checks themselves.
Check yourself
In the program above, Ana's account starts at 100 and ends at -400. Why did nothing stop the line a.balance = a.balance - 500a.balance = a.balance - 500;?
Ben's account starts at 50. Two lines outside the class change it. What does this print?
class Account: def __init__(self, owner, balance): self.owner = owner self.balance = balance
b = Account("Ben", 50)b.balance = b.balance - 80b.balance = b.balance + 20print(b.balance)class Account { String owner; int balance;
Account(String owner, int balance) { this.owner = owner; this.balance = balance; }}
public class Main { public static void main(String[] args) { Account b = new Account("Ben", 50); b.balance = b.balance - 80; b.balance = b.balance + 20; System.out.println(b.balance); }}A program has twenty places that change an account's balance. Where should the check that the balance never goes below zero live, so that no change can skip it?
- Say what goes wrong when any line of a program can write to an object's field: nothing checks the change, so a balance of 100 can become -400.
- Explain why an open field makes bugs hard to find: a wrong value could come from any of the places that change it.
- Say what encapsulation means: the object keeps its data to itself, and only its own methods, which check every change, may change it.