Python OOP क्या है? Class, Object, Constructor & Inheritance in Hindi | RBSE Class 12

इस लेख में: Python में Object-Oriented Programming (OOP), Class, Object, __init__() Constructor, self, Methods, Encapsulation, Inheritance और Polymorphism को आसान Hindi में examples के साथ समझेंगे।

OOP क्या है?

OOP का पूरा नाम Object-Oriented Programming है। यह programming का एक approach है जिसमें program को objects और classes के आधार पर organize किया जाता है।

Python में OOP का उपयोग बड़े और structured programs को व्यवस्थित तरीके से बनाने के लिए किया जा सकता है।

OOP के मुख्य Concepts

  • Class
  • Object
  • Constructor
  • Method
  • Inheritance
  • Encapsulation
  • Polymorphism
Exam Tip: RBSE Class 12 के लिए Class, Object, Constructor और Inheritance को विशेष रूप से अच्छी तरह समझें।

Class क्या है?

Class objects बनाने का एक blueprint या template होती है। इसमें data और उस data पर काम करने वाले methods को define किया जा सकता है।

Example

class Student:
    pass

यहाँ Student एक class है।

Object क्या है?

Object किसी class का instance होता है। Class को define करने के बाद उसके आधार पर objects create किए जा सकते हैं।

class Student:
    pass

student1 = Student()

print(student1)

यहाँ student1, Student class का object है।

Class और Object में अंतर

Class Object
Blueprint या template होती है। Class का instance होता है।
Class से objects बनाए जा सकते हैं। Object class के features का उपयोग करता है।
Example: Student Example: student1

Class में Attributes

Class या object से संबंधित data को attributes कहा जा सकता है।

class Student:
    name = "Rahul"
    age = 18

student1 = Student()

print(student1.name)
print(student1.age)

Output:

Rahul
18

self क्या है?

Python class के methods में self current object को refer करने के लिए उपयोग किया जाता है।

class Student:

    def display(self):
        print("This is a student")

student1 = Student()

student1.display()

यहाँ self उस object को refer करता है जिसके माध्यम से method call किया गया है।

Exam Tip: Python में instance methods का पहला parameter सामान्यतः self होता है।

Constructor क्या है?

Python में object create होने पर initialization के लिए __init__() method का उपयोग किया जाता है। इसे commonly constructor कहा जाता है।

class Student:

    def __init__(self):
        print("Object created")

student1 = Student()

Output:

Object created

__init__() में Parameters

हम constructor में parameters देकर object के attributes को initialize कर सकते हैं।

class Student:

    def __init__(self, name, age):
        self.name = name
        self.age = age

student1 = Student("Rahul", 18)

print(student1.name)
print(student1.age)

Output:

Rahul
18

Multiple Objects

class Student:

    def __init__(self, name, age):
        self.name = name
        self.age = age

student1 = Student("Rahul", 18)
student2 = Student("Amit", 17)

print(student1.name)
print(student2.name)

Output:

Rahul
Amit

Methods क्या हैं?

Class के अंदर define किए गए functions को methods कहा जाता है।

class Student:

    def greet(self):
        print("Welcome to Python")

student1 = Student()

student1.greet()

Method with Parameters

class Calculator:

    def add(self, a, b):
        print(a + b)

c = Calculator()

c.add(10, 20)

Output:

30

Instance Variables

Object से संबंधित variables को instance variables कहा जाता है। इन्हें सामान्यतः self के साथ define किया जाता है।

class Student:

    def __init__(self, name):
        self.name = name

student1 = Student("Rahul")
student2 = Student("Amit")

print(student1.name)
print(student2.name)

Class Variable

Class variable class से संबंधित होता है और class के objects द्वारा access किया जा सकता है।

class Student:

    school = "ABC School"

student1 = Student()
student2 = Student()

print(student1.school)
print(student2.school)

Instance Variable और Class Variable में अंतर

Instance Variable Class Variable
Object-specific data रखता है। Class-level data रखता है।
अलग-अलग objects के लिए अलग हो सकता है। सामान्यतः class के साथ shared होता है।
self के माध्यम से access किया जाता है। Class name के माध्यम से भी access किया जा सकता है।

Inheritance क्या है?

Inheritance OOP का ऐसा concept है जिसमें एक class दूसरी class के attributes और methods को inherit कर सकती है।

जिस class से features लिए जाते हैं उसे commonly Parent/Base class और जो class features inherit करती है उसे Child/Derived class कहा जाता है।

Example

class Animal:

    def eat(self):
        print("Animal eats")


class Dog(Animal):

    def bark(self):
        print("Dog barks")


dog1 = Dog()

dog1.eat()
dog1.bark()

Output:

Animal eats
Dog barks

Parent Class और Child Class

class Animal:
    pass

class Dog(Animal):
    pass

यहाँ Animal parent class और Dog child class है।

Method Overriding

यदि child class में parent class के method के समान नाम का method define किया जाए, तो child class का method parent method को override कर सकता है।

class Animal:

    def sound(self):
        print("Animal sound")


class Dog(Animal):

    def sound(self):
        print("Dog barks")


dog = Dog()

dog.sound()

Output:

Dog barks

super() Function

super() का उपयोग parent class के methods या constructor को access करने के लिए किया जा सकता है।

class Animal:

    def __init__(self):
        print("Animal constructor")


class Dog(Animal):

    def __init__(self):
        super().__init__()
        print("Dog constructor")


dog = Dog()

Output:

Animal constructor
Dog constructor

Inheritance के प्रकार

Python में inheritance के विभिन्न forms हो सकते हैं:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Single Inheritance

class Parent:
    def show(self):
        print("Parent")


class Child(Parent):
    pass


obj = Child()

obj.show()

Multiple Inheritance

जब एक child class एक से अधिक parent classes से inherit करती है, तो उसे multiple inheritance कहा जाता है।

class Father:

    def father_method(self):
        print("Father")


class Mother:

    def mother_method(self):
        print("Mother")


class Child(Father, Mother):
    pass


obj = Child()

obj.father_method()
obj.mother_method()

Multilevel Inheritance

class Grandparent:
    pass


class Parent(Grandparent):
    pass


class Child(Parent):
    pass

यहाँ inheritance का chain बनता है: Grandparent → Parent → Child

Encapsulation क्या है?

Encapsulation का अर्थ data और methods को एक class के अंदर organize करना तथा access को नियंत्रित करना है।

Python में naming conventions जैसे single underscore और double underscore का उपयोग attributes के intended access level को indicate करने के लिए किया जाता है।

class Student:

    def __init__(self):
        self.name = "Rahul"
        self._marks = 90
        self.__password = "1234"

यहाँ __password double underscore के साथ name-mangling का उदाहरण है।

Polymorphism क्या है?

Polymorphism का अर्थ broadly "many forms" है। OOP में एक ही interface या method name अलग objects के लिए अलग behavior दे सकता है।

class Dog:

    def sound(self):
        print("Bark")


class Cat:

    def sound(self):
        print("Meow")


animals = [Dog(), Cat()]

for animal in animals:
    animal.sound()

Output:

Bark
Meow

Class का Practical Example

class Student:

    def __init__(self, name, marks):
        self.name = name
        self.marks = marks

    def display(self):
        print("Name:", self.name)
        print("Marks:", self.marks)


student1 = Student("Rahul", 85)

student1.display()

Output:

Name: Rahul
Marks: 85

OOP के फायदे

  • Code को व्यवस्थित करने में मदद करता है।
  • Code reuse किया जा सकता है।
  • Inheritance से existing functionality को extend किया जा सकता है।
  • Large programs को manage करना आसान हो सकता है।
  • Data और related methods को एक unit में organize किया जा सकता है।

Important OOP Terms

Term Meaning
Class Object बनाने का blueprint/template
Object Class का instance
Attribute Object/Class से संबंधित data
Method Class के अंदर defined function
Constructor Object initialization के लिए __init__()
self Current object को refer करता है
Inheritance एक class से दूसरी class में features inherit करना
Encapsulation Data और methods को class में organize करना
Polymorphism एक interface/method के विभिन्न behaviors

RBSE Class 12 Important Questions

  1. OOP का पूरा नाम क्या है?
  2. Object-Oriented Programming क्या है?
  3. Class क्या है?
  4. Object क्या है?
  5. Class और Object में अंतर लिखिए।
  6. self क्या है?
  7. Constructor क्या है?
  8. __init__() method का उपयोग क्या है?
  9. Method क्या है?
  10. Instance Variable क्या है?
  11. Class Variable क्या है?
  12. Instance Variable और Class Variable में अंतर लिखिए।
  13. Inheritance क्या है?
  14. Parent Class और Child Class क्या हैं?
  15. Method Overriding क्या है?
  16. super() function का उपयोग क्या है?
  17. Inheritance के विभिन्न प्रकार लिखिए।
  18. Encapsulation क्या है?
  19. Polymorphism क्या है?
  20. Python में Class और Object का example लिखिए।

Frequently Asked Questions (FAQs)

Python OOP क्या है?

Python OOP यानी Object-Oriented Programming एक programming approach है जिसमें program को classes और objects के आधार पर organize किया जाता है।

Class क्या होती है?

Class एक blueprint या template होती है जिसके आधार पर objects बनाए जा सकते हैं।

Object क्या होता है?

Object किसी class का instance होता है।

__init__() क्या है?

__init__() Python class में object initialization के लिए commonly used special method है।

self क्या है?

self instance method में current object को refer करने के लिए उपयोग किया जाता है।

Inheritance क्या है?

Inheritance में एक class दूसरी class के attributes और methods को inherit कर सकती है।

Polymorphism क्या है?

Polymorphism का अर्थ है कि समान interface या method अलग objects के लिए अलग behavior दे सकता है।

अब अगला Topic पढ़ें

RBSE Python Study Tip: OOP में Class, Object, self और __init__() सबसे पहले अच्छी तरह समझें। इसके बाद Inheritance, Method Overriding, Encapsulation और Polymorphism की practice करें। Exam में Class और Object के छोटे programs तथा output-based questions पर विशेष ध्यान दें।
Official Reference:

Python Classes और OOP की विस्तृत जानकारी के लिए Python की official documentation देखें।

Python Classes Documentation