Python Chapter 7 — object-oriented programming
Priority key · Priorities guide emphasis; they do not remove taught scope.
Exam recall
Class defines; object instantiates. Trace state and return separately. Encapsulation controls the interface; inheritance reuses; overriding supports polymorphism.
Classes model state and behaviour priority/high
An object carries its current state between calls. Calling a method can change that state, return a result, or both. A class is the shared definition; two instances can use the same methods while holding different balances, names or settings.
Account class: defines owner, balance, deposit(), get_balance()
accountA → owner Ali, balance10
accountB → owner Bea, balance30
accountA.deposit(5) changes accountA to15; accountB stays30.A class defines a kind of object; an object is an instance. Attributes hold state, and methods define behaviour. __init__ initialises a newly created instance. self refers to the receiving instance in a method; it is not itself an attribute. self.name is an instance attribute. Separate instances normally hold separate instance state.
class Account:
def __init__(self, owner, balance):
self.__owner = owner
self.__balance = balance # Each instance receives its own balance.
def get_balance(self):
return self.__balance
def deposit(self, amount):
if amount <= 0: # Reject invalid input before changing stored state.
return False
self.__balance += amount
return Trueaccount = Account("Ali", 10) instantiates the class; account.deposit(5) calls an instance method, with self supplied automatically. Balance becomes 15. An invalid deposit leaves balance unchanged and returns False. Trace returned value and changed state separately.
A class-level attribute defined in the class body is shared through the class unless an instance shadows it. Put per-object mutable state such as each workout’s list of readings in __init__; a list defined only once in the class body can accidentally be shared. A getter returns state; a setter should validate before changing it. When a method receives another object, use that object’s interface, for example other.get_balance(), and distinguish it from self.
Encapsulation, inheritance and polymorphism priority/high
Encapsulation controls interaction with state; inheritance specialises a class; polymorphism selects behaviour through a common interface. These concepts can appear together, but one is not a definition of another.
Encapsulation groups state with operations and controls interaction through an interface. In the example, a deposit method enforces a positive-amount rule. Python’s double-underscore name mangling discourages accidental access/collision; it is not an impenetrable security boundary.
Inheritance defines a specialised subclass from a base class, reusing/extending suitable attributes and methods. Use it for an is-a relationship: a savings account is an account. A customer has an account; that is an association, not necessarily inheritance.
Polymorphism allows the same method call to invoke different appropriate implementations for different object types. Explain the actual overridden method and changed behaviour, not merely “many forms”. super().__init__(...) calls the base initialiser to set up inherited state rather than duplicating it carelessly.
Worked example — original priority/medium
class Ticket:
def __init__(self, base_price):
self.base_price = base_price
def price(self):
return self.base_price
class StudentTicket(Ticket):
def __init__(self, base_price, discount):
super().__init__(base_price) # Initialise the inherited part of this object.
self.discount = discount
def price(self):
return self.base_price * (1 - self.discount) # Override the price calculation.Ticket(20).price() returns 20; StudentTicket(20, 0.25).price() returns 15. Calling price() through a common interface produces type-appropriate behaviour. The subclass inherits the base structure and overrides the calculation. The contract assumes discount is a fraction in [0,1].
Class diagrams priority/high
Use separate compartments for class name, attributes and methods, showing types/parameters where requested. Common visibility marks: + public and - private. An inheritance arrow points from subclass towards the superclass, conventionally with a hollow triangle. Do not duplicate every inherited member in the subclass unless the required notation asks for it. Distinguish a class diagram from a diagram of particular objects and their current values.
Practice
Exam focus: class diagrams, inheritance and polymorphism recur in HCI 2022 modified Q6,2023 Q5,2024 Q8 and 2025 Q1; ASRJC 2025 Q4 uses devices. Read HCI 2025 Q1, PDF p.2 alongside 18A and ASRJC 2025 Q4, pp.3–4 alongside 18B to see the complete original attribute lists.
Analysis before a diagram: collect shared state in the base; place specialised state in subclasses; identify which operation has a shared name but different behaviour. Before coding, write each method’s parameters, returned value and state change separately. A constructor should initialise an object’s state, not accidentally create shared mutable state for every object.
18A — adapted from HCI 2025 Q1. Design a base Workout with date and duration and a method summary(). Cardio adds distance; Strength adds repetitions. State the inheritance relationships and give a concrete polymorphism example using summary. Draw the three class boxes with attributes and method signatures.
18B — adapted from ASRJC 2025 Q4. Device stores device_id and on/off state, initially off, with activate() setting it on. Camera inherits Device and stores recording, initially False; its overridden activate should turn it on and start recording. SmartCamera inherits Camera. Implement the classes and trace activation of one SmartCamera.
18C — original, extra practice. Add withdraw(amount) to Account: return False without change if amount is nonpositive or exceeds balance; otherwise subtract it and return True. Explain how this supports encapsulation.
Hints
18B: inherited initialisation must still occur. 18C: equality with balance is allowed.
Revision checklist
- 18.1 Distinguish class, object, instance, attribute, method and self.
- 18.2 Write a correctly indented class with an appropriate init method.
- 18.3 Instantiate objects and call methods using their interfaces.
- 18.4 Distinguish instance state from class-level attributes.
- 18.5 Explain encapsulation and information hiding without overstating Python access restrictions.
- 18.6 Use getter/setter methods and object parameters appropriately.
- 18.7 Write subclasses that initialise inherited state and add their own state.
- 18.8 Explain and demonstrate overriding and polymorphism in a scenario.
- 18.9 Draw class diagrams showing attributes, methods and inheritance.
- 18.10 Check that a method changes the intended object’s state and returns the requested result.
Visual revision mindmap

Open this mindmap and its text version · All 21 mindmaps
Your mindmap framework
Centre: Python Chapter 7 — object-oriented programming. Build the six branches below. For each subbranch, add a short definition, a labelled sample and one exam trap from memory; then check the chapter.
flowchart LR C["18 • Revision map"] C --> B0["Object model"] C --> B1["Construction and state"] C --> B2["Encapsulation"] C --> B3["Inheritance"] C --> B4["Polymorphism and diagrams"] C --> B5["Visual checks and mistakes"]
-
Object model
- Class versus object/instance.
- Attributes store state.
- Methods define behaviour.
- self is receiving instance, not an attribute.
-
Construction and state
- init initialises instance.
- Instantiate then call methods.
- Instance state versus class attributes.
- Mutable class-level value can be shared unintentionally.
-
Encapsulation
- Group state with operations.
- Controlled access through interface.
- Getter reads; setter validates.
- Python name mangling is not an impenetrable barrier.
-
Inheritance
- Is-a relationship; distinguish has-a.
- Superclass shared members.
- Subclass specialised members.
- super initialisation and method reuse.
-
Polymorphism and diagrams
- Same method call, type-appropriate implementation.
- Override a method and show concrete behaviour.
- Class compartments: name, attributes, methods.
- Visibility, types/signatures; inheritance arrow toward superclass.
-
Visual checks and mistakes
- Draw two Account instances with different balances.
- Trace method return separately from state change.
- Draw full Workout hierarchy from original question.
- Avoid: duplicate inherited state; missing base initialisation; methods changing wrong object.
Close the notes and test the map: explain one branch aloud, sketch its sample, then answer a linked practice question. Mark any missing link to revisit.
Source trail
9569 §2.3; school Object Oriented Programming.
HCI 2022 Q6; 2023 Q5; 2024 Q8; 2025 Q1; your OOP mistake-bank entries.
Source guide records provenance and original-paper locations.