11-15-2025, 10:43 PM
Chapter 18 — Object-Oriented Programming (Beginner Overview)
OOP (Object-Oriented Programming) lets you organise code around “objects” — things that have properties and behaviour.
At first it can feel strange, but once you understand it, everything clicks.
Games, apps, websites, simulations, AI systems — almost all major programs use OOP.
---
18.1 What Is an Object?
An object is a bundle of:
• data (attributes)
• behaviour (methods/functions)
Example in real life:
A **Dog** has:
• attributes → name, age, colour
• behaviours → bark(), eat(), sleep()
Programming copies this idea.
---
18.2 Creating a Class
A class is a blueprint for making objects.
This creates a “Dog type”, but it has no data yet.
---
18.3 The __init__ Method
This runs when you create (instantiate) an object.
self refers to the specific object created.
Create a dog:
Access attributes:
---
18.4 Adding Methods (Object Behaviours)
Use it:
Output:
---
18.5 Multiple Objects From One Class
Each object is separate.
---
18.6 Updating Attributes
---
18.7 Example — Simple Character Class
Use it:
---
18.8 Why OOP Is Useful
OOP makes your programs:
• more organised
• easier to expand
• cleaner to maintain
• great for large projects
• realistic (objects model real things)
---
18.9 Inheritance (Beginner Peek)
A class can reuse and extend another class.
Cat automatically has everything Animal has — but can also override behaviour.
---
18.10 Mini Project — Bank Account Class
Your class must include:
• __init__(owner, balance)
• deposit(amount)
• withdraw(amount)
• show_balance()
Example:
---
18.11 Challenge — RPG Fighter Class
Create a class with:
• name
• health
• attack_power
Functions:
• attack(other_character)
• heal(amount)
• status()
Example battle:
BONUS:
Add critical hits (random 1.5× damage).
---
18.12 Chapter Summary
• a class is a blueprint
• objects are created from that blueprint
• attributes = data
• methods = behaviour
• __init__ runs automatically
• self refers to the current object
• OOP creates bigger, cleaner, scalable programs
Next:
Chapter 19 — Practical Project: Building a Mini Python Game
This chapter turns everything learned so far into a small but exciting playable project.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
OOP (Object-Oriented Programming) lets you organise code around “objects” — things that have properties and behaviour.
At first it can feel strange, but once you understand it, everything clicks.
Games, apps, websites, simulations, AI systems — almost all major programs use OOP.
---
18.1 What Is an Object?
An object is a bundle of:
• data (attributes)
• behaviour (methods/functions)
Example in real life:
A **Dog** has:
• attributes → name, age, colour
• behaviours → bark(), eat(), sleep()
Programming copies this idea.
---
18.2 Creating a Class
A class is a blueprint for making objects.
Code:
class Dog:
passThis creates a “Dog type”, but it has no data yet.
---
18.3 The __init__ Method
This runs when you create (instantiate) an object.
Code:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = ageself refers to the specific object created.
Create a dog:
Code:
my_dog = Dog("Rex", 4)Access attributes:
Code:
print(my_dog.name)
print(my_dog.age)---
18.4 Adding Methods (Object Behaviours)
Code:
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(self.name + " says: Woof!")Use it:
Code:
d = Dog("Buddy")
d.bark()Output:
Code:
Buddy says: Woof!---
18.5 Multiple Objects From One Class
Code:
d1 = Dog("Max")
d2 = Dog("Bella")
d1.bark()
d2.bark()Each object is separate.
---
18.6 Updating Attributes
Code:
d = Dog("Luna")
d.age = 3---
18.7 Example — Simple Character Class
Code:
class Character:
def __init__(self, name, health):
self.name = name
self.health = health
def take_damage(self, amount):
self.health -= amount
print(f"{self.name} now has {self.health} HP")Use it:
Code:
hero = Character("Astra", 100)
hero.take_damage(15)---
18.8 Why OOP Is Useful
OOP makes your programs:
• more organised
• easier to expand
• cleaner to maintain
• great for large projects
• realistic (objects model real things)
---
18.9 Inheritance (Beginner Peek)
A class can reuse and extend another class.
Code:
class Animal:
def sound(self):
print("Some sound")
class Cat(Animal):
def sound(self):
print("Meow")Cat automatically has everything Animal has — but can also override behaviour.
---
18.10 Mini Project — Bank Account Class
Your class must include:
• __init__(owner, balance)
• deposit(amount)
• withdraw(amount)
• show_balance()
Example:
Code:
Account: Lee
Balance: £120---
18.11 Challenge — RPG Fighter Class
Create a class with:
• name
• health
• attack_power
Functions:
• attack(other_character)
• heal(amount)
• status()
Example battle:
Code:
Astra attacks Vorn for 12 damage!
Vorn now has 58 HP.BONUS:
Add critical hits (random 1.5× damage).
---
18.12 Chapter Summary
• a class is a blueprint
• objects are created from that blueprint
• attributes = data
• methods = behaviour
• __init__ runs automatically
• self refers to the current object
• OOP creates bigger, cleaner, scalable programs
Next:
Chapter 19 — Practical Project: Building a Mini Python Game
This chapter turns everything learned so far into a small but exciting playable project.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
