Objects: state, behaviour and an interface
Introduced| English | Meaning |
|---|---|
| object/ˈɒbdʒekt/ | An entity in a program with associated state and behaviour, often an instance of a class. |
| class/klæs/ | The template; an object is one instance. |
| state/steɪt/ | The current values of an object's attributes. |
| behaviour | The actions or responses provided by an object's methods. |
| instance/ˈɪnstəns/ | A particular occurrence or example; in object-oriented programming, an object created from a class. |
| encapsulation/ɪnˌkæpsjʊˈleɪʃn/ | In networking, wrapping data with protocol information; in OOP, grouping data with methods and controlling access through an interface. |
| invariant/ɪnˈveərɪənt/ | a condition about an object's state that must always remain true |
Objects for simulation, 1967
- In Oslo in the 1960s, Ole-Johan Dahl and Kristen Nygaard needed to simulate real-world systems, such as customers queuing at a counter. Their language Simula 67 let a programmer describe a class of thing — a customer — and create many objects from it, each with its own data.
- Simula introduced classes and objects; nearly every modern language follows. An object 对象 combines state 状态 (attributes) and behaviour 行为 (methods), defined by a class 类.
Which language introduced classes and objects in 1967?
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Dahl and Nygaard designed it for simulation.
Class and object
- A class is the template; an object is one instance.
Accountis a class; Ana's account and Ben's account are objects. - A constructor sets the starting state.
An object is an instance of a class.
Difficulty 1/5 · 100 coins + a small bonus on first completion today
The class is the template.
Encapsulation
- Encapsulation 封装: make attributes private and allow changes only through methods. The method checks the change is valid.
- The object's invariant 不变式 — a rule that is always true, such as "balance ≥ 0" — is protected.
What does encapsulation do?
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Methods validate changes.
Inheritance and polymorphism
- Inheritance:
SavingsAccount extends Accountreuses its code and adds interest. - Polymorphism: one method call,
account.monthEnd(), behaves differently for each subclass.
One class, separate object state
Trace which instance receives each call. Compare accepted and rejected operations while keeping both balances nonnegative.
Match each idea to its meaning.
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Reuse and flexibility.
Worked example: an Account class
public class Account {
private double balance;
public Account(double opening) { balance = opening; }
public void deposit(double amount) {
if (amount > 0) balance += amount;
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) { balance -= amount; return true; }
return false;
}
public double getBalance() { return balance; }
}
- Trace:
new Account(25),deposit(10),withdraw(8)→ balance 25 → 35 → 27. withdraw(100)returns false and the balance stays 27: the invariant balance ≥ 0 is preserved.
An Account starts at 25, deposits 10, then withdraws 8. What is the final balance?
Difficulty 2/5 · 140 coins + a small bonus on first completion today
25 + 10 − 8 = 27.
What does withdraw(100) do when the balance is 27?
Difficulty 1/5 · 100 coins + a small bonus on first completion today
The invariant is preserved.
The interface
- Other code uses only the public methods — the interface. The private details can change without breaking callers.
- That is why large programs are built from objects.
A public attribute breaks encapsulation. If balance were public, any code could set it to −500 and the class could not stop it. Keep state private and validate in methods.
Making balance public keeps the invariant safe.
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Any code could set it.
A class defines state and behaviour; objects are its instances. Encapsulation keeps attributes private and validates every change, protecting invariants; inheritance reuses code and polymorphism lets one call behave differently per subclass.
Design a class Thermostat with a private target temperature that must stay between 10 and 30 °C. Write its setter.
State, validation, invariant.