A class as a box
After this: Read one class box: its three parts, + and -, and the types written the Java way.
On this page · 6 sections · 3 checks
You have an idea for a program and want to show a friend the plan before you write any code. Which classes will there be? What will each one keep, and what will each one do? Code is a poor way to show this: it is long, and the plan hides among the details. A drawing on one page shows it at a glance.
One class, one box
Programmers draw that plan with boxes and lines. Each class is a box. The lines between boxes show how the classes are linked; lessons 2 and 3 teach the lines. This lesson is about one box.
A drawing of a program's classes and the links between them is called a . You can draw it before you write the code, as a plan, or after, to explain code that already exists.
Think of the bank account from the tutorial "Classes and objects". It keeps an owner and a balance, and it can deposit, withdraw, and tell you the balance. Here it is as a box. Press Run it, and the box fills one part at a time.
Press Run it to build the diagram one step at a time.
- classes
- 0
- relations
- 0
The three parts
Every box has three parts, from top to bottom:
- The name of the class:
BankAccount. - The fields: the data each object of the class keeps.
- owner: Stringsays there is a field namedownerand that it holds text. - The methods: what an object of the class can do.
+ deposit(amount: int)says there is a method nameddepositthat takes one input,amount, a whole number.
Leave the sign at the start of each line for the next section. After it, a field line is a name, a colon and a type. For a method, the inputs and their types go inside the brackets, and a type after the closing bracket is what the method gives back. + get_balance(): int+ getBalance(): int takes no input and returns a whole number. + deposit(amount: int) has no type after the bracket; in this learning path, that means the method returns nothing.
A box leaves out the code inside the methods, and usually leaves out the constructor too. It shows what a class keeps and what it can do, not how it does it.
Plus and minus
The sign at the start of each line says who may use that field or method. This is called :
| Mark | Name | Who may use it |
|---|---|---|
+ | public | any code in the program |
- | private | only the class's own methods |
# | protected | the class and the classes built on it |
So the box says: other code calls deposit and withdraw, and never changes balance itself. That is a design choice, and a good one: the account's own methods can check every change. The next tutorial, "Encapsulation", is about that choice in full. Python has no word for private. The - is still worth drawing: it is a promise that other code will leave the field alone, and the next tutorial shows the mark Python programmers use for it.Java writes it with the word private in front of the field, as in private int balance. Two Java details differ a little from the table. Java's protected also lets any class in the same package use the member, which in the one-file programs of this learning path means any class in the file. And a member with no word in front can be used by any class in the same package too, so the diagrams here draw it +.
One spelling for two languages
The diagrams in this learning path draw types the Java way, so one drawing serves both languages. Where Python's names differ, read them across with this table:
| In Python | In a diagram |
|---|---|
str | String |
int | int |
float | double |
list of text | List<String> |
dict from text to numbers | Map<String, Integer> |
The part inside < > says what the list or the map holds. Method names follow your language: switch the code language at the top of the page and the box above shows get_balancegetBalance.
Check yourself
In the BankAccount box, which part holds the line + withdraw(amount: int)?
What does the - in front of - balance: int say?
A class keeps a private field names, a list of names (a list of str valuesa List<String>). How does a diagram in this tutorial write that line?
Where these drawings come from
- Say what a class diagram is: a drawing of a program's classes, one box per class, with lines for the links between them.
- Read the three parts of a box from top to bottom: the class name, the fields, and the methods.
- Read a member line: the name, a colon and a type for a field; the inputs in brackets and an optional return type for a method.
- Say what + (public), - (private) and # (protected) in front of a line mean.
- Read the types the Java way in both languages: str is String, float is double, a list of text is List<String>.