Things with data and behaviour
After this: Say what an object is, make one, and watch two objects keep their own data.
On this page · 8 sections · 3 checks
A bank has thousands of accounts. Every account holds the same two things: the name of its owner and a balance. And every account allows the same actions: you can pay money in, and you can take money out as long as there is enough. The bank writes those rules once. Each account then keeps numbers of its own, and a deposit into Ana's account changes Ana's balance and nobody else's.
A program can be built the same way. This tutorial is about describing a kind of thing once, and then making as many of those things as you need, each with data of its own.
Data and behaviour together
So far, data and the functions that work on it have lived apart. A balance was a variable, and a deposit was a function you handed the balance to. With many accounts that gets awkward: a list of names, a list of balances, and a function that has to be told which position to change.
The other way is to keep each account's data and its actions together, as one thing. An is one such thing: it has data of its own, and actions that work on that data. Ana's account is an object. Ben's account is another object of the same kind, with its own data.
The smallest example, by hand
Take one account: Ana, with a balance of 100.
- Make the account. It holds the name Ana and the balance 100.
- Deposit 50 into it. Its balance becomes 150.
- Ask for its balance: 150.
Now add a second account: Ben, with 10. Deposit 50 into Ana's account. Ana's balance is 150 and Ben's is still 10. Nothing done to one account touches the other. That is the whole point of objects.
Watch it happen
Here is Ana's account in code. The lines at the top describe what every account holds and can do; lessons 2 and 3 show how to write them. Below them, the program makes one account, deposits 50 and prints the balance. Press Run it and step through.
class Account: def __init__(self, name, balance): self.name = name self.balance = balance
def deposit(self, amount): self.balance = self.balance + amount
a = Account("Ana", 100)a.deposit(50)print(a.balance)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
- The program starts at the top, reads the description, and moves on to line 917. The lines of the description run only when an account is made or used.
- Account("Ana", 100)new Account("Ana", 100) makes the account. A frame opens, as it does for a function, and fills the new object with Ana and 100. When it closes, the box
aholds the object. The band writes it as Account(name='Ana', balance=100)Account(name="Ana", balance=100): one box, with the object's two values inside it. a.deposit(50)is an action done to this account. The dot says which object, and the brackets give the amount. A frame opens again, and its first box, selfthis, is the account itself. Lesson 3 looks inside that frame. When it closes,aholds Account(name='Ana', balance=150)Account(name="Ana", balance=150).a.balancereads one value out of the object, and print shows150.
Two accounts
Now make two accounts and deposit into one of them. The task asks you to watch which box changes.
- Run the programRun it and step through. Watch two boxes appear in the Variables panel, a and b, each with its own name and balance inside. Then watch which of the two changes when a.deposit(50) runs.
class Account: def __init__(self, name, balance): self.name = name self.balance = balance
def deposit(self, amount): self.balance = self.balance + amount
a = Account("Ana", 100)b = Account("Ben", 10)a.deposit(50)print(a.balance)print(b.balance)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The boxes a and b each hold an object with its own name and balance. a.deposit(50) changed a alone, so the output is 150 and then 10. If the program had said b.deposit(50), the frame would have opened with Ben's account in it, and b would have changed instead.
The code
class Account: def __init__(self, name, balance): self.name = name self.balance = balance
def deposit(self, amount): self.balance = self.balance + amount
a = Account("Ana", 100)a.deposit(50)print(a.balance)150
class Account { String name; int balance;
Account(String name, int balance) { this.name = name; this.balance = balance; }
void deposit(int amount) { balance = balance + amount; }}
public class Main { public static void main(String[] args) { Account a = new Account("Ana", 100); a.deposit(50); System.out.println(a.balance); }}150
Read the two halves separately. The top half is the description: what every account holds (a name and a balance) and what it can do (deposit). The bottom half is a program like the ones you have written before, except that one of its values is an account.
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, step through the traces above instead, and paste the code into a Java tool on your computer when you want to run it for real.
Change the 50 to 25 and run it again. Then add a second account of your own under the first one, deposit into it, and print its balance.
Check yourself
The program runs a = Account("Ana", 100) (in Java, Account a = new Account("Ana", 100);). What does the box a hold afterwards?
a holds Ana's account with 100, and b holds Ben's with 10. The program runs a.deposit(50). What does b.balance hold now?
Two accounts, and a deposit on the second one. What does this print?
class Account: def __init__(self, name, balance): self.name = name self.balance = balance
def deposit(self, amount): self.balance = self.balance + amount
a = Account("Ana", 100)b = Account("Ben", 10)b.deposit(5)print(a.balance)print(b.balance)class Account { String name; int balance;
Account(String name, int balance) { this.name = name; this.balance = balance; }
void deposit(int amount) { balance = balance + amount; }}
public class Main { public static void main(String[] args) { Account a = new Account("Ana", 100); Account b = new Account("Ben", 10); b.deposit(5); System.out.println(a.balance); System.out.println(b.balance); }}Honesty notes
- Say what an object is: one thing in the program that keeps data of its own and has actions that work on that data.
- Read the box of an object in the trace band: one box, with the object's values written inside it.
- Make an object from its description and call one of its actions with a dot, as in a.deposit(50).
- Say that two objects made from the same description keep their own data: a deposit on one leaves the other untouched.