Python Object Oriented Programming (OOP) - Complete Guide
text1. Object Oriented Programming Kya Hai?
OOP ek programming style hai jisme hum real-world entities ko classes aur objects ki madad se represent karte hain. Python me OOP ke basic concepts hain Classes, Objects, Inheritance, Encapsulation, Polymorphism aur Abstraction. OOP se aapka code modular, reusable aur easy to maintain hota hai.
2. Class Aur Object
Class ek blueprint hoti hai jisse hum objects banate hain. Object class ka ek instance hai jisme data (attributes) aur behaviour (methods) hota hai.
class Dog: # Constructor to initialize attributes def init(self, name, age): self.name = name self.age = age text # Method that describes the dog def description(self): return f"{self.name} is {self.age} years old." def speak(self, sound): return f"{self.name} says {sound}" Object banaye dog1 = Dog("Buddy", 4) dog2 = Dog("Lucy", 2) print(dog1.description()) # Buddy is 4 years old. print(dog2.speak("Woof")) # Lucy says Wooftext
3. Constructor (`__init__` Method)
__init__
method special method hota hai jo object creation ke samay call hota hai. Isme attributes initialize hote hain.
4. Instance Variable aur Class Variable
Instance variable har object ka apna hota hai, jabki class variable sab objects ke liye common hota hai.
class Car: wheels = 4 # class variable text def __init__(self, make, model): self.make = make # instance variable self.model = model car1 = Car("Toyota", "Corolla") car2 = Car("Honda", "Civic") print(car1.wheels) # 4 print(car2.model) # Civictext
5. Inheritance
Inheritance ke through ek class dusri class ke features use kar sakti hai.
class Animal: def init(self, name): self.name = name def speak(self): pass class Dog(Animal): def speak(self): return f"{self.name} says Woof" class Cat(Animal): def speak(self): return f"{self.name} says Meow" dog = Dog("Buddy") cat = Cat("Kitty") print(dog.speak()) # Buddy says Woof print(cat.speak()) # Kitty says Meowtext
6. Encapsulation
Encapsulation se hum data ko private bana sakte hain jise direct access nahi kiya ja sakta. Python me iske liye double underscore prefix use hota hai.
class Person: def init(self, name, age): self.name = name self.__age = age # private attribute text def get_age(self): return self.__age def set_age(self, age): if age > 0: self.__age = age else: print("Age should be positive.") p = Person("Neha", 25) print(p.get_age()) # 25 p.set_age(30) print(p.get_age()) # 30 p.set_age(-5) # Age should be positive.text
7. Polymorphism
Polymorphism ka matlab hai alag-alag classes me ek hi function ka alag tarike se kaam karna.
class Bird: def fly(self): print("Flying in the sky") class Sparrow(Bird): def fly(self): print("Flying low") class Eagle(Bird): def fly(self): print("Flying high") for bird in (Bird(), Sparrow(), Eagle()): bird.fly()text
8. Abstraction
Abstraction me hum complexity chhupakar sirf essential features dikhate hain. Python me iske liye abc
module aur abstract classes use karte hain.
from abc import ABC, abstractmethod class Vehicle(ABC): @abstractmethod def wheels(self): pass class Bike(Vehicle): def wheels(self): return 2 class Car(Vehicle): def wheels(self): return 4 print(Bike().wheels()) # 2 print(Car().wheels()) # 4text
9. Summary
Python OOP se aap powerful modular code likh sakte hain jo reusable ho. Classes aur objects se real-world modeling asaan hoti hai. Inheritance aur polymorphism se code flexible banta hai. Encapsulation data protection deti hai aur abstraction complexity ko hide karta hai.
Practice ke saath aap OOP ke in concepts me proficient ban sakte hain aur bade projects develop kar sakte hain.