Object-Oriented Thinking
MohammadRezaMahdian – November 2025
November 15, 2025
What is OOP?
Imagine you're building with LEGO. Every brick is an object — it has a shape, a color, and it does something.
OOP is just that: organizing your code like a LEGO set.
Why bother?
Your program starts small. Then it grows. And suddenly? Chaos.
OOP keeps things clean, fixable, and easy to grow. Fewer headaches down the road.
The 4 Core Ideas
- Class — the blueprint
- Polymorphism — same command, different behavior
- Inheritance — borrow from a parent
- Composition — build from smaller pieces
Class = Blueprint
A class says: “Here’s what something is, and what it can do.”
class Dog:
def __init__(self, name, breed):
self.name = name # data
self.breed = breed
def bark(self): # action
print("Woof!")
Object = Real Thing
Now make a real dog:
my_dog = Dog("Rex", "German Shepherd")
my_dog.bark()
# → Woof!
Has-A = Composition
Ask: “What does this thing have?” → That’s composition.
A Car has an Engine. Take the engine out? No car.
class Engine:
def start(self):
print("Engine running")
class Car:
def __init__(self):
self.engine = Engine() # Car HAS an Engine
def drive(self):
self.engine.start()
Strong bond — parts live and die with the whole.
Is-A = Inheritance
Ask: “What is this thing?” → That’s inheritance.
A PoliceCar is a Car.
class PoliceCar(Car):
def siren(self):
print("Wee-woo!")
Now it can drive() and siren().
Hide the Mess (Encapsulation)
Don’t let anyone poke the insides.
class BankAccount:
def __init__(self):
self.__balance = 500 # hidden
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
Only through deposit() and get_balance().
Getters & Setters
def get_speed(self):
return self.speed
def set_speed(self, value):
if value >= 0:
self.speed = value
Class vs Object
| Class | Object |
|---|---|
| Blueprint | Real instance |
| One definition | Many copies |
| In code | In memory |
Composition vs Aggregation
- Composition: Strong → Engine dies with Car
- Aggregation: Weak → Players can leave Team
class Player:
pass
class Team:
def __init__(self):
self.players = []
def add_player(self, p):
self.players.append(p)
How to Design
- What does it have? → attributes
- What does it do? → methods
Cat → name, age → meow(), sleep()
Objects Talk
car.drive()
account.deposit(100)
Keep It Clean
Hide complexity. Reuse. Build step by step.
Bottom Line
OOP isn’t magic. It’s thinking about code like real life.
Makes big programs manageable.
MohammadRezaMahdian – Nov 2025
Top comments (0)