A levels Computer Science · Programming and data
Object-oriented programming
1Classes and objects
A class is a template that defines attributes (data) and methods (behaviour). An object is an instance of a class, created by calling its constructor.
class Account:
def __init__(self, owner, balance=0): # constructor
self.owner = owner
self.balance = balance def deposit(self, amount):
self.balance += amount
a = Account("Lee") # instantiation
a.deposit(50)
print(a.balance) # 50
Each object has its own copy of the attributes, so two Account objects can have different balances.
Check yourself
What is instantiation?
Show the answer
Creating an object from a class
Making an instance.
2Encapsulation
Encapsulation bundles data with the methods that act on it, and hides the data from outside code. Attributes are made private, and other code uses public getter and setter methods.
class Account:
def __init__(self, owner):
self.__balance = 0 # private (name starts with __) def get_balance(self):
return self.__balance
def withdraw(self, amount):
if 0 < amount <= self.__balance:
self.__balance -= amount
Benefits: the class can check values before changing them (here, no overdraft), and its inner workings can change without breaking code that uses it.
Check yourself
Why are attributes usually made private?
Show the answer
So they can only be changed through methods that can check the values
Controlled access.
3Inheritance
Inheritance lets a subclass (child) take on the attributes and methods of a superclass (parent), and add or change its own. It models an "is a" relationship.
Inheritance. Dog and Cat get name and speak() from Animal, and each overrides speak() with its own version.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "..."class Dog(Animal): # Dog inherits from Animal
def speak(self):
return "Woof"
Inheritance reduces repeated code: shared features are written once in the superclass.
Check yourself
Which relationship does inheritance represent?
Show the answer
"is a", for example a Dog is an Animal
Subclass is a kind of superclass.
4Polymorphism and overriding
Polymorphism means objects of different classes can be treated the same way, and each responds in its own way.
Overriding is when a subclass redefines a method it inherited:
animals = [Dog("Rex"), Cat("Tom"), Animal("?")]
for a in animals:
print(a.speak()) # Woof, Meow, ...
The same call, a.speak(), runs a different method depending on the object's class. New subclasses can be added without changing the loop.
Check yourself
What is overriding?
Show the answer
A subclass redefining a method it inherited from its superclass
Same name, new behaviour.
5Association, aggregation and composition
Objects often link to other objects in a "has a" relationship (association).
| Kind | Meaning | Example | Class diagram symbol |
|---|---|---|---|
| aggregation | the parts can exist without the whole | a Team has Players | hollow diamond |
| composition | the parts are destroyed with the whole | a House has Rooms | filled diamond |
In a class diagram, inheritance is shown by an arrow with a hollow triangle pointing at the superclass, private members with − and public members with +.
Check yourself
A Library has Books, and the books still exist if the library closes. What kind of association is this?
Show the answer
Aggregation
Parts survive.
6Design principles
Three widely used principles:
- Encapsulate what varies: keep parts that are likely to change in their own class.
- Favour composition over inheritance: building objects from parts is often more flexible than deep inheritance trees.
- Program to interfaces, not implementations: code should depend on what methods an object offers, not on its exact class.
Advantages of OOP: code is reusable, classes can be tested on their own, and programs are easier to maintain and extend. Disadvantages: it can be slower and use more memory, and it can be overkill for small programs.
Check yourself
Which is an advantage of object-oriented programming?
Show the answer
Classes can be reused and tested independently
Modular.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera