Python me OOP (Object-Oriented Programming) kya hai?


OOP ek programming style hai jisme hum real-world cheezein ko code me represent karte hain. Jaise real life me ek "Car" ka concept hota hai — har car ke kuch properties hote hain (color, speed) aur kuch kaam hote hain (start, stop). OOP me isi tarah hum Classes aur Objects banate hain.

OOP ke 4 main pillars hain :-

Is page me yeh sab sikhenge :-



1. Class aur Object

Class ek blueprint (naqsha) hai — jaise ek ghar ka naqsha. Object us naqshe se bana asli ghar hai. Class ek baar likhte hain, objects baar baar bana sakte hain.

Syntax
class ClassName:
    # attributes aur methods

obj = ClassName()   # object banana

Example 1: Pehli class banana aur object create karna

Python
In [1]:
1
Output


2. __init__() Constructor

__init__() ek special method hai jo object banate waqt automatically call hota hai. Isme hum object ki initial properties (attributes) set karte hain. self parameter us particular object ka reference hota hai.

Example 1: __init__() se attributes set karna

Python
In [1]:
1
Output

Example 2: Attributes directly access aur modify karna

Python
In [1]:
1
Output


3. Instance Variables aur Methods

Instance variables har object ke apne alag hote hain. Instance methods woh functions hain jo object ki properties pe kaam karte hain — inhein hamesha self parameter milta hai.

Example 1: Bank Account — realistic example

Python
In [1]:
1
Output


4. Class Variables aur Class Methods

Class variables saare objects ke beech share hoti hain — ek object badalta hai toh sabko dikhta hai. @classmethod decorator se class methods banate hain — isme cls parameter hota hai (self nahi).

Example 1: Class variable — kitne objects bane track karna

Python
In [1]:
1
Output


5. Inheritance (Virasat)

Inheritance matlab ek class doosri class ki properties aur methods inherit kar leti hai. Parent class (Base class) ki saari cheezein Child class (Derived class) ko mil jaati hain — aur wo apni nayi cheezein bhi add kar sakti hai.

Example 1: Animal → Dog, Cat — basic inheritance

Python
In [1]:
1
Output

Example 2: Person → Student → GraduateStudent — multi-level

Python
In [1]:
1
Output


6. Method Overriding

Jab child class me parent class ka same naam ka method likhte hain toh child ka method parent wale ko override kar deta hai — yaani child ka method chal jaata hai.

Example 1: Shape → Circle, Rectangle — area method override

Python
In [1]:
1
Output


7. super() Function

super() se child class, parent class ke methods aur __init__ ko call kar sakti hai. Yeh tab useful hota hai jab child class me kuch extra karna ho parent ke upar se.

Example 1: super() se parent ka __init__ extend karna

Python
In [1]:
1
Output


8. Encapsulation — Private Variables

Encapsulation matlab data ko bahar se direct access hone se rokna. Python me double underscore (__) se variable ko private banate hain. Private variables sirf class ke andar se access ho sakte hain.

Example 1: Private variables — __ (double underscore)

Python
In [1]:
1
Output


9. Polymorphism

Polymorphism matlab "same naam, alag kaam". Alag alag classes ke objects par same method call karo — har class apne hisaab se kaam karegi.

Example 1: speak() method — alag animals alag awaaz

Python
In [1]:
1
Output

Example 2: len() — built-in polymorphism

Python
In [1]:
1
Output


10. Special (Dunder) Methods

Python me kuch special methods hote hain jo double underscore (__) se shuru aur khatam hote hain — inhe Dunder methods kehte hain. Yeh methods Python ke built-in operations ko customize karne ke kaam aate hain.

Example 1: __str__, __len__ aur __add__

Python
In [1]:
1
Output

Example 2: __eq__ aur __gt__ — comparison operators customize karna

Python
In [1]:
1
Output



Quick Summary — OOP ek nazar me

ConceptKaamExample
classBlueprint bananaclass Dog:
objectClass ka instanced = Dog()
__init__Constructor — attributes setdef __init__(self, name):
selfCurrent object ka referenceself.name = name
Instance varHar object ka apnaself.age = 20
Class varSabka sharedcount = 0
@classmethodClass methoddef f(cls):
InheritanceParent se properties milnaclass Cat(Animal):
super()Parent class call karnasuper().__init__()
OverrideParent method rewrite karnadef area(self): ...
EncapsulationPrivate variablesself.__balance
PolymorphismSame naam, alag kaamobj.speak()
__str__print() ke liye stringdef __str__(self):
__len__len() ke liyedef __len__(self):