🧠 Part 1: What is Object-Oriented Programming (OOP)?
📘 Definition:
Object-Oriented Programming (OOP) ek programming style hai jisme real-world objects
ki tarah data aur functions ko organize kiya jata hai.
🔸 Example from Real Life:
● Ek "Car" hai → uska color, model, speed → ye uske attributes hain
● Car ke actions: drive(), stop() → ye uske methods hain
ir
( OOP ek programming paradigm hai jo code ko objects ke through organize karta hai.
Objects real world entities jaise car, person, account ko represent karte hain )
nA
Main Concepts:
● Class
aO
● Object
● Constructor
mz
● self
● Inheritance
Ha
● Encapsulation
● Polymorphism
● Abstraction
🔍 Why OOP?
● Code reuse hota hai (reusability)
● Asaani se maintain hota hai (maintainability)
● Code real world ki tarah hota hai (realistic modeling)
🧠 Part 2: What is a Class?
📘 Definition:
Class ek blueprint hoti hai object banane ke liye. Ye sirf ek template hota hai, real cheez
nahi.
ir
🔸 Example:
python
CopyEdit
nA
class Person:
aO
pass
🧵 Explanation:
mz
● class → keyword hai jisse class banti hai
● Person → class ka naam hai (capital letter se likhna convention hai)
Ha
● pass → iska matlab abhi class me koi functionality nahi hai (empty class)
(Class: Ek blueprint ya template hai jo objects ke properties aur behaviors ko define karta
hai.)
✅ Note:
Class banane ke baad hum usse object banate hain — tab hi wo kaam karegi.
🧠 Part 3: What is an Object?
📘 Definition:
Object ek instance hota hai class ka — yaani class ki copy jo memory me banti hai.
🔸 Example:
python
CopyEdit
class Person:
def greet(self):
print("Hello, I am a person.")
# Create object
p1 = Person()
ir
# Call method
[Link]()
nA
🧵 Line-by-Line Explanation:
aO
● class Person: → Class banayi
● def greet(self): → Method define kiya (function inside class)
mz
● print(...) → Method ka kaam: message print karna
● p1 = Person() → Object banaya class se
Ha
● [Link]() → Object se method call kiya
(Object: Ek instance hai jo class se create hota hai aur apne properties aur behaviors ko
work karwata hai.)
Example
class Car:
pass
my_car = Car()
Is example mein, Car ek class hai aur my_car ek object hai jo Car class se create hua hai.
✅ Note:
Object banne ke baad hi class ka code execute hota hai.
🧠 Part 4: The self Keyword
📘 Definition:
ir
nA
self ka matlab hota hai current object. Har method me self pehla argument hota hai,
jiske through object ke data ko access karte hain.
🔸 Example:
aO
python
CopyEdit
class Animal:
mz
def speak(self):
print("Animal is speaking")
a1 = Animal()
Ha
[Link]()
🧵 Explanation:
● def speak(self): → self object a1 ko refer kar raha hai
● Jab [Link]() call hota hai, Python andar hi andar speak(a1) call karta hai
✅ Rule:
Har method me pehla parameter self zaroori hota hai (except static methods, jo baad
me padhenge)
🧠 Part 5: Constructor (__init__ method)
📘 Definition:
Constructor ek special method hota hai jo object create hote hi automatically call hota
hai.
Attributes (Properties)
1. Attributes: Objects ke properties hain jo unki characteristics ko define karte hain.
2. __init__ method: Ek special method hai jo object creation ke time par call hota hai aur
ir
attributes ko initialize karta hai.
Example
class Car: nA
aO
def _init_(self, color, model):
[Link] = color
mz
[Link] = model
Ha
my_car = Car("Red", "Toyota")
print(my_car.color) # Output: Red
print(my_car.model) # Output: Toyota
Is example mein, color aur model attributes hain jo Car class ke objects ke properties ko
define karte hain.
🔸 Syntax:
python
CopyEdit
def __init__(self, ...):
🔸 Example:
python
CopyEdit
class Student:
def __init__(self, name, age):
[Link] = name
[Link] = age
def show(self):
print("Name:", [Link])
ir
print("Age:", [Link])
[Link]()
nA
s1 = Student("Hamza", 21)
🧵 Explanation:
aO
● __init__ → Constructor method
mz
● [Link] = name → Object ke andar name store kiya
● s1 = Student(...) → Object bana, constructor auto call hua
Ha
● [Link]() → Data print kiya
Methods (Behaviors)
1. Methods: Objects ke behaviors hain jo unke actions ko define karte hain.
2. Method definition: Ek function hai jo class ke andar define hota hai aur object ke
attributes ko access kar sakta hai.
Example
class Car:
def _init_(self, color, model):
[Link] = color
[Link] = model
def start_engine(self):
print("Engine started!")
my_car = Car("Red", "Toyota")
ir
my_car.start_engine() # Output: Engine started!
Is example mein, start_engine ek method hai jo Car class ke objects ke behavior ko
nA
define karta hai.
aO
🧠 Part 6: Instance Variables vs Class Variables
mz
🔸 Instance Variable:
Ha
● Har object ka apna data hota hai
● self.variable_name se banate hain
🔸 Class Variable:
● Sab objects ke liye common hota hai
● Class ke andar directly likhte hain
🔸 Example:
python
CopyEdit
class Car:
wheels = 4 # class variable
def __init__(self, brand):
[Link] = brand # instance variable
c1 = Car("Toyota")
c2 = Car("Suzuki")
print([Link]) # Toyota
print([Link]) # Suzuki
print([Link]) # 4
print([Link]) # 4
ir
🧵 Explanation:
nA
● wheels = 4 → class variable (common for all cars)
aO
● [Link] = brand → instance variable (different for each car)
✅ Part 7: Types of Methods in Python
mz
Python OOP me 3 types ke methods hote hain:
Ha
Type Use hota hai...
Instance Object ke data pe kaam karne ke
Method liye
Class Method Class ke level pe kaam karne ke
liye
Static Method General utility ke liye
🔹 1. Instance Method (most common)
📘 Kaam: Object ke variables ko access karta hai
python
CopyEdit
class Dog:
def __init__(self, name):
[Link] = name
def bark(self): # instance method
print(f"{[Link]} is barking")
d = Dog("Tommy")
[Link]()
🧵 Explanation:
ir
● [Link] → object ka name
nA
● bark() method object ke name pe kaam kar raha hai
● Call: [Link]() → Tommy is barking
aO
🔹 2. Class Method
mz
📘 Kaam: Class ke variables ko access karta hai
Ha
🔑 Use: Jab aapko class-level info chahiye ho
python
CopyEdit
class Dog:
species = "Canine" # class variable
@classmethod
def show_species(cls): # class method
print("Species:", [Link])
Dog.show_species()
🧵 Explanation:
● @classmethod → batata hai ye class method hai
● cls → class ko refer karta hai
● [Link] → class variable access kiya
Yeh code Python mein class variables aur class methods ka example hai.
Class Variables
ir
- species = "Canine" line ek class variable define karti hai jo Dog class ke liye common
hai.
nA
- Class variables ko sabhi instances share karte hain.
aO
Class Methods
- @classmethod decorator ek special decorator hai jo method ko class method mein
convert karta hai.
mz
- Class methods ko class ke naam se call kiya ja sakta hai, bina instance create kiye.
- Class methods mein cls parameter hota hai jo class ko represent karta hai.
Ha
Code Explanation
class Dog:
species = "Canine" # class variable
@classmethod
def show_species(cls): # class method
print("Species:", [Link])
Dog.show_species()
- Dog class define kiya gaya hai jisme species class variable hai.
- show_species method ek class method hai jo species variable ko print karta hai.
- Dog.show_species() line show_species method ko call karti hai aur "Species: Canine"
print karti hai.
ir
Benefits of Class Methods
nA
- Class methods ko class ke naam se call kiya ja sakta hai, bina instance create kiye.
- Class methods class variables ko access kar sakte hain.
aO
🔹 3. Static Method
mz
📘 Kaam: Na object se na class se — bas utility ka kaam karta hai
Ha
python
CopyEdit
class Math:
@staticmethod
def add(a, b):
print("Sum is:", a + b)
[Link](5, 3)
🧵 Explanation:
● @staticmethod → static method banata hai
● Kisi bhi self ya cls ki zarurat nahi
● Sirf general kaam ke liye hota hai
Yeh code Python mein static methods ka example hai.
Static Methods
- @staticmethod decorator ek special decorator hai jo method ko static method mein
convert karta hai.
- Static methods ko class ke naam se call kiya ja sakta hai, bina instance create kiye.
- Static methods mein self parameter nahi hota hai, kyunki yeh methods class ke
ir
instances se independent hote hain.
nA
Code Explanation
aO
class Math:
@staticmethod
mz
def add(a, b):
print("Sum is:", a + b)
Ha
[Link](5, 3)
- Math class define kiya gaya hai jisme add static method hai.
- add method do numbers ko add karta hai aur unka sum print karta hai.
- [Link](5, 3) line add method ko call karti hai aur "Sum is: 8" print karti hai.
Benefits of Static Methods
- Static methods ko class ke naam se call kiya ja sakta hai, bina instance create kiye.
- Static methods class-level functionality implement karne ke liye useful hote hain, jaise
ki utility methods.
✅ Part 8: Inheritance
📘 Definition:
ir
Ek class doosri class se features inherit kar leti hai — jaise ek beta apne baap ki cheezein
le leta hai.
🔸 Syntax:
python
nA
aO
CopyEdit
class ChildClass(ParentClass):
🔸 Example:
mz
python
CopyEdit
Ha
class Animal:
def sound(self):
print("Some sound")
class Dog(Animal):
pass
d = Dog()
[Link]()
🧵 Explanation:
● Dog(Animal) → Dog ne Animal class ke methods inherit kiye
● sound() Dog me likha nahi, fir bhi call ho gaya
✅ Why Inheritance?
● Code reuse hota hai
● Hierarchy create hoti hai
● Extend karna easy hota hai
ir
nA
✅ Part 9: Encapsulation
aO
📘 Definition:
Data ko hide karna aur access control dena encapsulation kehlata hai.
mz
🔒 Example:
python
CopyEdit
Ha
class Account:
def __init__(self):
self.__balance = 1000 # private variable
def show_balance(self):
print("Balance is:", self.__balance)
a = Account()
a.show_balance()
🧵 Explanation:
● __balance → private ban gaya (double underscore)
● show_balance() → proper method se access kiya
Yeh code Python mein private variables aur encapsulation ka example hai.
Private Variables
- __balance = 1000 line ek private variable define karti hai jo Account class ke andar hi
accessible hai.
- Private variables ko double underscore (__) se prefix kiya jata hai, jo Python mein name
mangling ka ek tarika hai.
ir
nA
Encapsulation
- Encapsulation ek OOP concept hai jo data ko hide karne aur uske access ko control
karne ke liye use hota hai.
aO
- Account class mein __balance variable ko private karke, hum is variable ko directly
access hone se rok sakte hain.
mz
Code Explanation
Ha
class Account:
def __init__(self):
self.__balance = 1000 # private variable
def show_balance(self):
print("Balance is:", self.__balance)
a = Account()
a.show_balance()
- Account class define kiya gaya hai jisme __balance private variable hai.
- show_balance method ek public method hai jo __balance variable ko print karta hai.
- a = Account() line Account class ka ek instance create karti hai.
- a.show_balance() line show_balance method ko call karti hai aur "Balance is: 1000"
print karti hai.
Benefits of Encapsulation
ir
- Encapsulation data ko secure karta hai aur uske access ko control karta hai.
nA
- Encapsulation code ko more maintainable aur flexible banata hai.
aO
Example Use Case
- Encapsulation ka use karke hum banking systems, financial applications, aur anya
sensitive data ko handle karne wale systems mein data ko secure kar sakte hain.
mz
Python mein variables aur methods ke access modifiers ko samajhne ke liye, humein
public, protected, aur private ke concepts ko samajhna hoga.
Ha
Public
- Public variables aur methods ko kisi bhi jagah se access kiya ja sakta hai.
- Public variables aur methods ko define karne ke liye koi special syntax nahi hai.
Protected
- Protected variables aur methods ko single underscore (_) se prefix kiya jata hai.
- Protected variables aur methods ko class ke andar aur subclass mein access kiya ja
sakta hai, lekin yeh convention hai ki inhein directly access nahi karna chahiye.
Private
- Private variables aur methods ko double underscore (__) se prefix kiya jata hai.
- Private variables aur methods ko class ke andar hi access kiya ja sakta hai, aur yeh
name mangling ke through internally change kiye jate hain.
Example
ir
class MyClass:
nA
def __init__(self):
self.public_var = "Public"
aO
self._protected_var = "Protected"
self.__private_var = "Private"
mz
def public_method(self):
Ha
print("Public method")
def _protected_method(self):
print("Protected method")
def __private_method(self):
print("Private method")
obj = MyClass()
print(obj.public_var) # Output: Public
obj.public_method() # Output: Public method
print(obj._protected_var) # Output: Protected
obj._protected_method() # Output: Protected method
# print(obj.__private_var) # Error: AttributeError
ir
# obj.__private_method() # Error: AttributeError
nA
- Is example mein, hum public variables aur methods ko directly access kar sakte hain.
- Protected variables aur methods ko bhi directly access kiya ja sakta hai, lekin yeh
aO
convention hai ki inhein directly access nahi karna chahiye.
- Private variables aur methods ko directly access nahi kiya ja sakta hai, aur yeh name
mangling ke through internally change kiye jate hain.
mz
✅ Note:
Ha
Encapsulation ka matlab hai direct access na dena, proper method ke through dena.
✅ Part 10: Polymorphism
📘 Definition:
Same method name → different behavior in different classes
🔸 Example:
python
CopyEdit
class Cat:
def sound(self):
print("Meow")
class Dog:
def sound(self):
print("Bark")
def animal_sound(animal):
[Link]()
c = Cat()
ir
d = Dog()
nA
animal_sound(c)
animal_sound(d)
🧵 Explanation:
aO
● sound() method dono classes me alag kaam kar raha hai
mz
● Function animal_sound() ne same method ko call kiya, lekin result alag mila
Yeh code Python mein polymorphism ka example hai.
Ha
Polymorphism
- Polymorphism ek OOP concept hai jo objects ko different forms mein use karne ki ijazat
deta hai.
- Is example mein, Cat aur Dog classes dono mein sound method hai, lekin yeh methods
different implementation ke साथ hain.
Code Explanation
class Cat:
def sound(self):
print("Meow")
class Dog:
def sound(self):
print("Bark")
ir
def animal_sound(animal):
[Link]()
c = Cat()
nA
aO
d = Dog()
mz
animal_sound(c) # Output: Meow
animal_sound(d) # Output: Bark
Ha
- Cat aur Dog classes define kiye gaye hain jisme sound method hai.
- animal_sound function ek parameter animal leta hai aur uske sound method ko call
karta hai.
- c aur d objects ko animal_sound function ke saath pass kiya gaya hai.
How it works
- Jab animal_sound(c) call kiya jata hai, toh c object ka sound method call hota hai, jo
"Meow" print karta hai.
- Jab animal_sound(d) call kiya jata hai, toh d object ka sound method call hota hai, jo
"Bark" print karta hai.
✅ Part 11: Abstraction
📘 Definition:
Sirf important cheezein dikhana, details chhupana — ye abstraction hai.
🧪 Python me abstraction karne ke liye:
ir
nA
● abc module ka use hota hai
● Abstract class me incomplete methods hote hain
aO
🔸 Example:
python
mz
CopyEdit
from abc import ABC, abstractmethod
class Shape(ABC):
Ha
@abstractmethod
def area(self):
pass
class Circle(Shape):
def area(self):
print("Circle area")
c = Circle()
[Link]()
🧵 Explanation:
● Shape abstract class hai
● area() method define nahi kiya gaya (abstract)
● Circle ne area() define karke use kiya
● Jab tak abstract method complete na ho, object nahi bana sakte
Yeh code Python mein abstract classes aur abstract methods ka example hai.
Abstract Classes
- Abstract classes ek type ki class hai jo directly instantiate nahi ho sakti.
ir
- Abstract classes ko dusre classes ke liye blueprint ke roop mein use kiya jata hai.
nA
Abstract Methods
aO
- Abstract methods ek type ki method hai jo abstract class mein define kiya jata hai aur
concrete implementation ke bina hota hai.
- Abstract methods ko @abstractmethod decorator se mark kiya jata hai.
mz
Code Explanation
Ha
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def area(self):
print("Circle area")
c = Circle()
[Link]() # Output: Circle area
- Shape class ek abstract class hai jo ABC class se inherit karta hai.
ir
- area method ek abstract method hai jo Shape class mein define kiya gaya hai.
- Circle class Shape class se inherit karta hai aur area method ko implement karta hai.
nA
- c = Circle() line Circle class ka ek instance create karti hai.
- [Link]() line area method ko call karti hai aur "Circle area" print karti hai.
aO
Benefits of Abstract Classes
mz
- Abstract classes code ko more structured aur maintainable banate hain.
- Abstract classes inheritance aur polymorphism ko promote karte hain.
Ha
Example Use Case
- Abstract classes ka use karke hum game development mein different game objects ko
same interface ke saath use kar sakte hain.
- Abstract classes ka use karke hum banking systems mein different account types ko
same interface ke saath use kar sakte hain.
Important Point
- Agar hum Shape class ka instance create karne ki koshish karte hain, toh yeh error
throw karega kyunki Shape class ek abstract class hai.
✅ Summary Table:
Concept Use / Purpose
Class Blueprint hota hai
Object Class ka instance (copy)
self Current object ko refer karta hai
ir
Constructor Object banate hi auto call hota hai
Class Var nA
Instance Var Har object ka alag data
Sab objects ka common data
aO
Instance Object ke saath kaam
Method
Class Method Class-level kaam
mz
Static Method Utility method, kisi se related nahi
Inheritance Code reuse, Parent → Child class
Ha
Encapsulation Data hiding with access control
Polymorphism Same method, different behavior
Abstraction Show what’s necessary, hide internal
detail