π¦ 1. What is OOP?
OOP = a way of organizing code around βobjects.β
Objects = things that have properties (data) and behaviors (functions).
Real-life analogy
A Car:
- Properties β color, speed, brand
- Behaviors β start(), stop(), accelerate()
In OOP, you'd define a Car class and create many cars (objects/instances) from it.
π¦ 2. Class & Object
β Class
A class is a blueprint β like a recipe.
β Object
An object is the actual thing created from that blueprint.
Example:
class Car:
pass
my_car = Car() # object
π¦ 3. Attributes (Variables inside objects)
Attributes store data about the object.
Example:
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
my_car = Car("Tesla", "Red")
print(my_car.brand) # Tesla
print(my_car.color) # Red
Real-life analogy
__init__() = when you build a car at the factory β you set brand, color, model.
π¦ 4. Methods (Functions inside a class)
Methods define behaviors of objects.
class Car:
def __init__(self, brand):
self.brand = brand
def start(self):
print(self.brand, "is starting...")
tesla = Car("Tesla")
tesla.start()
π¦ 5. The self keyword
self refers to the current object.
Real-life analogy:
Think of βmy ownβ β my color, my brand, my speed.
π¦ 6. Encapsulation (Hiding data)
We keep some data private to protect it.
class BankAccount:
def __init__(self, balance):
self.__balance = balance # private
def get_balance(self):
return self.__balance
You cannot directly access __balance from outside.
This prevents accidental misuse β like ATM preventing direct access to internal cash counters.
π¦ 7. Inheritance (Child class getting features of parent class)
Just like:
- Dog πΆ and Cat π± inherit from Animal
Example:
class Animal:
def speak(self):
print("Animal makes a sound")
class Dog(Animal):
def speak(self):
print("Dog barks")
d = Dog()
d.speak() # Dog barks
Real-life analogy:
A child inherits features from parent β height, hair color, etc.
π¦ 8. Polymorphism (Same method name, different behavior)
Example:
class Bird:
def fly(self):
print("Bird is flying")
class Airplane:
def fly(self):
print("Airplane is flying")
for obj in [Bird(), Airplane()]:
obj.fly()
Real life:
βdrive()β means different things for:
- car
- bike
- truck
π¦ 9. Abstraction (Show only important things, hide details)
Real-life example:
When you drive a car, you donβt see the engine or circuits β only steering & pedals.
In code, abstraction hides complexity.
Example using abstract class:
from abc import ABC, abstractmethod
class Payment(ABC):
@abstractmethod
def pay(self):
pass
π¦ 10. Full Real-Life Example β ATM System
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
def withdraw(self, amount):
if amount <= self.__balance:
self.__balance -= amount
return "Withdraw successful"
return "Insufficient funds"
def get_balance(self):
return self.__balance
# Creating objects
ali_acc = BankAccount("Ali", 5000)
sara_acc = BankAccount("Sara", 7000)
print(ali_acc.withdraw(2000))
print(sara_acc.deposit(1000))
print(ali_acc.get_balance())
This example uses:
β attributes
β private variables
β methods
β constructor
β encapsulation
π¦ 11. Mini Exercises (Your turn!)
π‘ Send me your code and Iβll review it!
1. Create a class Student
With attributes:
- name
- age
- marks
Add method:
-
is_pass()β returns True if marks > 40.
2. Create a class Car
Attributes: brand, speed
Methods: accelerate(), brake()
3. Create two classes
-
Animal(parent) -
Dog(child) β override a speak() method
Top comments (0)