Definition
Object-Oriented Programming (OOP) is a paradigm based on the concept of objects, which are instances of classes. A class defines attributes (state) and methods (behaviour).
Classes and Objects
Key Point
| OOP term | Meaning |
|---|---|
| Class | Blueprint defining attributes and methods |
| Object | Instance created from a class |
| Instantiation | Process of creating an object |
Encapsulation
Definition
Encapsulation is the practice of keeping an object's state private, accessible only through its public methods. This protects object integrity and hides complexity.
Worked Example
OOP Example: Bank Account
Encapsulation benefits:
• Data protection from invalid modifications
• Controlled access through methods
• Implementation details hidden
• Easier to maintain and modify
pseudocode
class BankAccount:
// Private attributes (encapsulated)
private accountNumber
private balance
private ownerName
// Public methods (controlled access)
public procedure new(number, name):
accountNumber = number
ownerName = name
balance = 0
end procedure
public procedure deposit(amount):
if amount > 0 then
balance = balance + amount
end if
end procedure
public function getBalance():
return balance
end function
end class
// Usage
myAccount = new BankAccount("12345", "Alice")
myAccount.deposit(100)
print(myAccount.getBalance()) // 100
// myAccount.balance = 1000000 // ERROR! Private attribute
• Data protection from invalid modifications
• Controlled access through methods
• Implementation details hidden
• Easier to maintain and modify
Constructors, Get Methods and Set Methods
Key Point
| Term | Meaning |
|---|---|
| Attribute | A variable that belongs to an object, such as balance |
| Method | A subroutine that belongs to a class, such as deposit() |
| Constructor | The method that runs when an object is instantiated and gives its attributes their starting values; in OCR pseudocode it is named new |
| Get method | A public method that returns the value of a private attribute, such as getBalance() |
| Set method | A public method that changes a private attribute, so the new value can be checked before it is stored |
Inheritance
Definition
Inheritance lets a new class (the subclass, or child class) take on all the attributes and methods of an existing class (the superclass, or parent class). The subclass can then add attributes and methods of its own.
Polymorphism
Definition
Polymorphism means that the same method name can behave differently depending on the class of the object it is called on. A subclass achieves this by overriding a method it has inherited: it defines a method with the same name but different code.
Worked Example
Inheritance and Overriding: Savings Account
• SavingsAccount inherits accountNumber, balance, ownerName, deposit() and getBalance() from BankAccount
• super.new() runs the constructor of the superclass, so its attributes are set up without repeating the code
• deposit() is overridden: calling deposit() on a SavingsAccount object runs the new version, while a BankAccount object still runs the original
• balance is private to BankAccount, so the subclass reads it through getBalance() and not directly
pseudocode
class SavingsAccount inherits BankAccount:
private interestRate
public procedure new(number, name, rate):
super.new(number, name)
interestRate = rate
end procedure
// Overrides the deposit method inherited from BankAccount
public procedure deposit(amount):
if amount >= 10 then
super.deposit(amount)
end if
end procedure
public procedure addInterest():
super.deposit(getBalance() * interestRate / 100)
end procedure
end class
mySavings = new SavingsAccount("67890", "Ben", 2)
mySavings.deposit(5) // Ignored: the overriding method needs at least 10
mySavings.deposit(200) // Balance is now 200
mySavings.addInterest() // 2% of 200 is added: balance is now 204
print(mySavings.getBalance()) // 204
• super.new() runs the constructor of the superclass, so its attributes are set up without repeating the code
• deposit() is overridden: calling deposit() on a SavingsAccount object runs the new version, while a BankAccount object still runs the original
• balance is private to BankAccount, so the subclass reads it through getBalance() and not directly
Exam Tip
Exam Tips for Programming Techniques:
• Know the three basic constructs (sequence, selection, iteration)
• Understand recursion: base case, recursive case, stack behavior
• Be able to compare recursion vs iteration
• Know scope: local vs global variables and their trade-offs
• Understand parameter passing: by value vs by reference
• Know IDE features: stepping, breakpoints, variable watch
• Understand OOP basics: classes, objects, encapsulation
• Be able to trace recursive algorithms
• Know the three basic constructs (sequence, selection, iteration)
• Understand recursion: base case, recursive case, stack behavior
• Be able to compare recursion vs iteration
• Know scope: local vs global variables and their trade-offs
• Understand parameter passing: by value vs by reference
• Know IDE features: stepping, breakpoints, variable watch
• Understand OOP basics: classes, objects, encapsulation
• Be able to trace recursive algorithms