🟢 Beginner  ·  Lesson 20

Object-Oriented Programming in Python

Python में Object-Oriented Programming

What is OOP?

Object-Oriented Programming (OOP) organizes code around objects — things that have data (attributes) and actions (methods). A class is the blueprint; an object is a real item made from it.

💡 Real example

A "Student" class is the blueprint. Aman and Riya are two objects (students) made from that blueprint, each with their own name and marks.

Class and Object

Python – class.py
class Dog:
    sound = "Bark"      # attribute shared by all dogs

d1 = Dog()              # create an object
print(d1.sound)
Bark

class Dog is the blueprint; d1 = Dog() makes one real dog object.

__init__ Constructor

__init__ runs automatically when an object is created. It sets up each object's own data. self refers to the current object.

Python – init.py
class Student:
    def __init__(self, name, marks):
        self.name = name
        self.marks = marks

s1 = Student("Aman", 95)
print(s1.name, s1.marks)
Aman 95

Program 1: Student Class

Python – student.py
class Student:
    def __init__(self, name, roll, marks):
        self.name = name
        self.roll = roll
        self.marks = marks

s1 = Student("Aman", 1, 88)
s2 = Student("Riya", 2, 95)
print(s1.name, "scored", s1.marks)
print(s2.name, "scored", s2.marks)
Aman scored 88 Riya scored 95

One class, two independent objects — each keeps its own name, roll and marks.

Program 2: Methods (Behaviour)

A method is a function inside a class that acts on the object's data.

Python – method.py
class Student:
    def __init__(self, name, marks):
        self.name = name
        self.marks = marks

    def result(self):
        return "PASS" if self.marks >= 33 else "FAIL"

s1 = Student("Aman", 75)
print(s1.name, ":", s1.result())
Aman : PASS

Program 3: Inheritance

Inheritance lets a class reuse another class's code. The child gets everything from the parent and can add more.

Python – inherit.py
class Person:
    def __init__(self, name):
        self.name = name
    def greet(self):
        print("Hi, I am", self.name)

class Teacher(Person):       # Teacher inherits Person
    def teach(self):
        print(self.name, "is teaching")

t = Teacher("Mr. Rao")
t.greet()    # from Person
t.teach()    # from Teacher
Hi, I am Mr. Rao Mr. Rao is teaching

Teacher(Person) means Teacher inherits Person — it can use greet() for free.

Program 4: Bank Account (Encapsulation)

Python – bank.py
class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        if amount > self.balance:
            print("Insufficient funds")
        else:
            self.balance -= amount

acc = BankAccount("Aman", 1000)
acc.deposit(500)
acc.withdraw(300)
print("Balance:", acc.balance)
Balance: 1200

The data (balance) and the actions on it (deposit, withdraw) live together — this is encapsulation.

Four Pillars of OOP

PillarMeaning
EncapsulationData + methods bundled in one class
InheritanceChild class reuses parent code
PolymorphismSame method name, different behaviour
AbstractionHide complex details, show only what is needed

Common Mistakes

  • Forgetting self as the first parameter of methods.
  • Forgetting brackets when creating an object: Student vs Student().
  • Mixing up the class (blueprint) with the object (real item).

Practice Tasks

  1. Make a Car class with brand and speed, and a method to show details.
  2. Add a method to the Student class that prints the grade.
  3. Create a Animal parent and Dog/Cat children that override a sound() method.
  4. Build a simple Rectangle class with an area() method.

Summary

  • OOP organizes code into classes (blueprints) and objects (real items).
  • __init__ sets up each object; self is the current object.
  • Methods are functions inside a class; inheritance reuses code.
  • Four pillars: encapsulation, inheritance, polymorphism, abstraction.

OOP क्या है?

Object-Oriented Programming (OOP) code को objects के इर्द-गिर्द organize करता है — ऐसी चीज़ें जिनमें data (attributes) और actions (methods) होते हैं। class blueprint है; object उससे बनी असली चीज़।

💡 Real example

"Student" class blueprint है। Aman और Riya उस blueprint से बने दो objects (students) हैं, हर एक का अपना name और marks।

Class और Object

Python – class.py
class Dog:
    sound = "Bark"      # सभी dogs का साझा attribute

d1 = Dog()              # object बनाएं
print(d1.sound)
Bark

class Dog blueprint है; d1 = Dog() एक असली dog object बनाता है।

__init__ Constructor

__init__ object बनते ही अपने आप चलता है। यह हर object का अपना data set करता है। self current object को दर्शाता है।

Python – init.py
class Student:
    def __init__(self, name, marks):
        self.name = name
        self.marks = marks

s1 = Student("Aman", 95)
print(s1.name, s1.marks)
Aman 95

Program 1: Student Class

Python – student.py
class Student:
    def __init__(self, name, roll, marks):
        self.name = name
        self.roll = roll
        self.marks = marks

s1 = Student("Aman", 1, 88)
s2 = Student("Riya", 2, 95)
print(s1.name, "scored", s1.marks)
print(s2.name, "scored", s2.marks)
Aman scored 88 Riya scored 95

एक class, दो स्वतंत्र objects — हर एक अपना name, roll और marks रखता है।

Program 2: Methods (Behaviour)

Method class के अंदर का function है जो object के data पर काम करता है।

Python – method.py
class Student:
    def __init__(self, name, marks):
        self.name = name
        self.marks = marks

    def result(self):
        return "PASS" if self.marks >= 33 else "FAIL"

s1 = Student("Aman", 75)
print(s1.name, ":", s1.result())
Aman : PASS

Program 3: Inheritance

Inheritance एक class को दूसरी class का code reuse करने देता है। Child को parent से सब मिलता है और वह और जोड़ सकता है।

Python – inherit.py
class Person:
    def __init__(self, name):
        self.name = name
    def greet(self):
        print("Hi, I am", self.name)

class Teacher(Person):       # Teacher, Person को inherit करता है
    def teach(self):
        print(self.name, "is teaching")

t = Teacher("Mr. Rao")
t.greet()    # Person से
t.teach()    # Teacher से
Hi, I am Mr. Rao Mr. Rao is teaching

Teacher(Person) मतलब Teacher, Person को inherit करता है — वह greet() मुफ़्त में use कर सकता है।

Program 4: Bank Account (Encapsulation)

Python – bank.py
class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        if amount > self.balance:
            print("Insufficient funds")
        else:
            self.balance -= amount

acc = BankAccount("Aman", 1000)
acc.deposit(500)
acc.withdraw(300)
print("Balance:", acc.balance)
Balance: 1200

Data (balance) और उस पर actions (deposit, withdraw) एक साथ रहते हैं — यही encapsulation है।

OOP के चार स्तंभ

स्तंभमतलब
EncapsulationData + methods एक class में bundle
InheritanceChild class parent का code reuse करे
Polymorphismएक ही method नाम, अलग behaviour
Abstractionजटिल details छुपाना, सिर्फ ज़रूरी दिखाना

सामान्य गलतियाँ

  • methods के पहले parameter self भूलना।
  • object बनाते समय brackets भूलना: Student बनाम Student()
  • class (blueprint) और object (असली चीज़) में confusion।

Practice Tasks

  1. brand और speed वाली Car class बनाएं, और details दिखाने वाला method।
  2. Student class में grade print करने वाला method जोड़ें।
  3. Animal parent और Dog/Cat children बनाएं जो sound() override करें।
  4. area() method वाली एक simple Rectangle class बनाएं।

सारांश

  • OOP code को classes (blueprints) और objects (असली items) में organize करता है।
  • __init__ हर object set करता है; self current object है।
  • Methods class के अंदर के functions हैं; inheritance code reuse करता है।
  • चार स्तंभ: encapsulation, inheritance, polymorphism, abstraction।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n

💻 Live Code Editor

Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें. Agar load na ho to naye tab mein kholें.