Computer Science · Topic Cheatsheet
Topic 7 · Object-Oriented Programming (HL)
22 key results accumulated across 2 chapters.
Class
Ch 1
BLUEPRINT — defines attributes + methods. Does nothing on its own.
Object / Instance
Ch 1
Each separate THING created from a class. `d1 = Dog('Rex')`. Many objects per class.
__init__
Ch 1
Constructor — runs automatically when object is created. Sets up initial attributes via `self.x = ...`.
self
Ch 1
Reference to the current object. `self.name` is this object's name; different objects have different selves.
Attribute
Ch 1
Data stored INSIDE an object. e.g. self.balance, self.name. Each object has its own values.
Method
Ch 1
Function defined INSIDE a class. Called as `obj.method()`. First parameter is always `self`.
Inheritance
Ch 1
`class Sub(Super)` — Sub IS-A Super and inherits Super's attributes + methods.
Override
Ch 1
Subclass defines a method with the SAME NAME as the parent → subclass version is used for its objects.
Polymorphism
Ch 1
Same method call → different behaviour depending on object's actual class. Run-time resolution.
4 OOP pillars
Ch 1
Encapsulation · Abstraction · Inheritance · Polymorphism.
Encapsulation
Ch 2
Bundle data + methods together. HIDE internals (private attributes) + expose CONTROLLED interface (public methods).
Private (Python)
Ch 2
Single underscore `_x` = convention. Double underscore `__x` = name-mangled (effectively private).
Accessor (getter)
Ch 2
Method that RETURNS a private attribute's value. Read-only access.
Mutator (setter)
Ch 2
Method that SETS a private attribute, usually with VALIDATION (e.g. reject negative balance).
Data integrity
Ch 2
Encapsulation benefit — validation in setters prevents invalid states.
Implementation hiding
Ch 2
Can change internals (e.g. data structure used) WITHOUT breaking external code that uses the public interface.
Composition (has-a)
Ch 2
Object CONTAINS another as an attribute. `Car has-a Engine`. Flexible, replaceable.
Inheritance (is-a)
Ch 2
Subclass IS A kind of superclass. `Dog is-a Animal`. Use only when relationship is genuinely is-a.
Rule of thumb
Ch 2
Prefer COMPOSITION over INHERITANCE unless IS-A is genuinely true. Composition is more flexible.
Abstraction
Ch 2
Expose simple INTERFACE; hide complex implementation. Lets multiple implementations satisfy same interface.
Open-Closed Principle
Ch 2
Code should be OPEN for extension (new subclasses) but CLOSED for modification (existing code unchanged). Polymorphism enables it.
Common exam trap
Ch 2
Confusing override (same name, replaces parent) with overload (same name, different parameters — NOT supported by Python directly).