Introduction
OOP में Class के बाद सबसे महत्वपूर्ण concept है Object
Class केवल एक blueprint होती है, लेकिन actual data और functionality object में होती है।
यानी program में real काम object के द्वारा ही होता है।
Object क्या होता है
Object class का instance होता है।
जब हम class से कोई real entity बनाते हैं, उसे object कहा जाता है।
Real-life समझ
- Class → Car
- Object → Toyota, Honda
- Class → Student
- Object → Rahul, Amit
Object कैसे बनाते हैं
object_name = ClassName()
Example
class Student:
name = "Rahul"
s1 = Student()
print(s1.name)
Output
Rahul
Object के द्वारा data access करना
Object की मदद से हम class के variables और methods access करते हैं।
class Student:
name = "Rahul"
s1 = Student()
print(s1.name)
Object के द्वारा method call करना
class Student:
def greet(self):
print("Hello Student")
s1 = Student()
s1.greet()
Multiple Objects
एक class से कई objects बनाए जा सकते हैं।
class Student:
def __init__(self, name):
self.name = name
s1 = Student("Rahul")
s2 = Student("Amit")
print(s1.name)
print(s2.name)
Object Properties (Attributes)
Object के अंदर जो data होता है, उसे attributes कहते हैं।
class Student:
def __init__(self, name):
self.name = name
s1 = Student("Rahul")
print(s1.name)
Object को modify करना
Object की value change भी की जा सकती है।
s1.name = "Amit"
print(s1.name)
Object delete करना
del s1
Real-life Example: Student object
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def show(self):
print(self.name, self.marks)
s1 = Student("Rahul", 80)
s2 = Student("Amit", 90)s1.show()
s2.show()
Real-life Example: Mobile object
class Mobile:
def __init__(self, brand):
self.brand = brand
def call(self):
print(self.brand, "calling")
m1 = Mobile("Samsung")
m1.call()
Object और Class में अंतर
| Class | Object |
|---|---|
| Blueprint | Instance |
| Structure define करता है | Real data store करता है |
| एक बार define होता है | कई बार create हो सकता है |
Important Points
- Object class से बनता है
- Object में data और methods होते हैं
- Multiple objects possible हैं
- Object के द्वारा ही program run होता है
Common mistakes
- object create करना भूल जाना
- method को class से directly call करना
- attribute access में गलती करना
Mini Program
class Person:
def __init__(self, name):
self.name = name
p1 = Person("Amit")
print(p1.name)
Summary
Object OOP का core concept है,
जो class को real-world में implement करता है।