2.1.1 · HinglishOOP Fundamentals

Class vs object — blueprint vs instance

1,387 words6 min readRead in English

2.1.1 · Coding › OOP Fundamentals


HUM kis baare mein baat kar rahe hain?


YEH distinction kyun exist karti hai?

Blueprint ko instance se alag kyun kiya jaata hai?

  1. Reuse — description ek baar likho, bahut saare objects stamp karo.
  2. Independent state — har object apna data rakhta hai; ek ko change karne se doosre corrupt nahi hote.
  3. Shared behaviour — methods ek baar (class par) store hoti hain, isliye 1000 objects mein se har ek code ki 1000 copies store nahi karta.

Inhe kaise banate aur use karte hain?

class Dog:                 # blueprint begins
    species = "Canis"      # CLASS attribute (shared by all dogs)
 
    def __init__(self, name, age):   # the constructor
        self.name = name             # INSTANCE attribute (per object)
        self.age  = age
 
    def bark(self):                  # behaviour
        return f"{self.name} says Woof!"
 
d1 = Dog("Rex", 3)         # instantiation -> an OBJECT
d2 = Dog("Fido", 5)        # another OBJECT, independent state

Instantiation step by step kaise kaam karta hai:

  1. Dog("Rex", 3) Python se request karta hai ki Dog type ka ek blank object create karo.
  2. Python __init__ ko call karta hai jahan self = naya blank object hota hai.
  3. self.name = name data ko us object mein likhta hai.
  4. Taiyaar object ka reference wapas hand over ho jaata hai aur d1 mein store ho jaata hai.
Figure — Class vs object — blueprint vs instance

Worked Examples


Common Mistakes


Recall Feynman: 12-saal ke bacche ko samjhao

Ek class cookie cutter ki tarah hai. Cutter khud cookie nahi hai — woh shape hai. Jab bhi tum usse dough mein press karte ho tum ek cookie (object) paate ho. Saari cookies ka same shape hota hai (same class), lekin tum har ek par alag toppings daal sakte ho (alag data). Ek cookie phenkne se doosri ko kuch nahi hota, aur tumhare paas zyada cookies banane ke liye cutter abhi bhi hai.


Flashcards

Class kya hoti hai?
Ek blueprint/template jo data (attributes) aur behaviour (methods) bundle karta hai; yeh kisi cheez ka description deta hai lekin khud runtime instance nahi hota.
Object kya hota hai?
Ek concrete instance jo class se bana ho, memory occupy karta ho, aur instance attributes ki apni copy rakhta ho.
Instantiation kya hai?
Class se object create karne ki act, jaise Dog("Rex", 3), jo __init__ trigger karta hai.
Instance attributes kahan rehte hain?
Individual object par (via self.x = ... likhe jaate hain), har object ke liye alag.
Class attributes kahan rehte hain?
Class par khud, saare instances ke beech shared, jab tak kisi instance par shadow na ho jaayein.
d1.age change karne se d2.age kyun affect nahi hota?
Har object ke paas instance attributes ke liye apni independent memory hoti hai.
Do equal-dikhte objects ke liye a is b kya test karta hai?
Identity (same memory location) — yeh False hoga do alag se instantiate kiye objects ke liye chahe data equal ho.
Class likhne se koi object create hota hai kya?
Nahi — yeh sirf ek blueprint define karta hai; objects tab aate hain jab tum class ko call karte ho.
Methods class par kyun store hoti hain, har object par kyun nahi?
Code ki ek copy saare instances ke beech share karne ke liye, memory bachane ke liye.

Connections

  • Constructors and __init__ — instantiation par object kaise initialise hota hai.
  • self and instance methodsself methods ko ek specific object se kaise bind karta hai.
  • Class vs instance attributes — shared-vs-own data ka distinction.
  • Encapsulation — class mein data+behaviour bundle karna kyun matter karta hai.
  • Identity vs Equality (is vs ==) — objects ko distinguish karna.
  • Memory model and references — objects actually kahan rehte hain.

Concept Map

describes template for

instantiation via

creates

stores once

holds

owns copy of

calls

run on

shared across

enables

keeps

so changing one

Class - blueprint

Object - instance

__init__ constructor

Methods - shared behaviour

Class attribute - shared

Instance attributes - per object

Reuse - many from one

Independent state

Does not affect others