Introduction To Python (Chapter: 3) | EduNotes

Object-Oriented Programming (OOP) in Python

Object-Oriented Programming (OOP) ek programming paradigm hai jisme program ko objects ke form me design kiya jata hai.

Object real-world cheezon ko represent karta hai, jaise: Student, Car, Mobile, Employee. Python me OOP ka use karke hum real-life problems ko easily model kar sakte hain.

Basic Concepts of OOP

OOP mainly 4 core concepts par based hoti hai:

  • Class
  • Object
  • Encapsulation
  • Inheritance

Advanced Concepts: Polymorphism, Abstraction

1. Class (Blueprint / Design)

Class ek blueprint hoti hai jiske according objects banaye jaate hain.

Class batati hai:

  • Object me kya data hoga (variables)
  • Object kya kaam karega (functions)

Simple Example:

class Student:
    pass
    

Yahan Student sirf ek design hai, real cheez nahi.

2. Object (Real Entity)

Object class ka real instance hota hai.

  • Class = Design
  • Object = Us design se bana real item

Example:

s1 = Student()
    

Yahan s1 ek object hai jo Student class se bana hai.

3. Encapsulation (Data + Methods Together)

Encapsulation ka matlab hai data aur functions ko ek hi class ke andar bandh karna.

Isse:

  • Data secure rehta hai
  • Code organized hota hai

Example:

class Student:
    def set_data(self, name, marks):
        self.name = name
        self.marks = marks

    def show_data(self):
        print(self.name, self.marks)

s1 = Student()
s1.set_data("Rahul", 85)
s1.show_data()
    

name, marks = Data
set_data(), show_data() = Methods
Sab kuch ek hi class me hai — isi ko Encapsulation kehte hain.

4. Inheritance (Reuse of Code)

Inheritance ka matlab hai ek class dusri class ki properties use kare.

Isse:

  • Code reuse hota hai
  • Duplication kam hoti hai

Example:

class Person:
    def show_name(self):
        print("Name: Amit")

class Student(Person):
    def show_role(self):
        print("Role: Student")

s = Student()
s.show_name()
s.show_role()
    

Person = Parent class
Student = Child class
Student class ne Person class ke features inherit kiye hain.

Why OOP is Important?

  • Real-world problems easily represent hote hain
  • Code reusable hota hai
  • Large programs manage karna easy hota hai
  • Security aur structure better hota hai

OOP vs Normal Programming

Normal Programming OOP
Function-based Object-based
Code repetition zyada Code reuse possible
Less secure More secure
Small programs ke liye Large programs ke liye