Basic OOP Concepts in Python
Introduction
Object Oriented Programming (OOP) केवल classes और objects तक सीमित नहीं है।
इसके कुछ basic concepts होते हैं, जो programming को और ज्यादा powerful और flexible बनाते हैं।
ये concepts real-world problems को efficiently solve करने में मदद करते हैं।
Basic OOP Concepts
Python में मुख्य OOP concepts हैं:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
1. Encapsulation
Definition
Encapsulation का मतलब है data (variables) और methods (functions) को एक ही class के अंदर bind करना।
Example
class Student:
def __init__(self, name):
self.name = name
def show(self):
print(self.name)
s1 = Student("Rahul")
s1.show()
Explanation
यहाँ data (name) और method (show) एक ही class में हैं।
इसे ही encapsulation कहते हैं।
2. Inheritance
Definition
Inheritance का मतलब है एक class दूसरी class की properties और methods को use कर सकती है।
Example
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
d = Dog()
d.speak()
d.bark()
Explanation
Dogclass नेAnimalclass को inherit किया है- इसलिए
Dogदोनों methods use कर सकता है
3. Polymorphism
Definition
Polymorphism का मतलब है एक function का अलग-अलग forms में काम करना।
Example
class Dog:
def sound(self):
print("Bark")
class Cat:
def sound(self):
print("Meow")
for animal in [Dog(), Cat()]:
animal.sound()
Explanation
- Same method name (
sound) - Different output → different behavior
4. Abstraction
Definition
Abstraction का मतलब है complex details को hide करके केवल जरूरी information दिखाना।
Example
class Car:
def start(self):
print("Car started")
c = Car()
c.start()
Explanation
User को यह नहीं पता कि अंदर क्या process हुआ,
उसे केवल result दिखता है → यही abstraction है।
Real-life Example
- ATM machine → abstraction (inner process hidden)
- Mobile phone → encapsulation (features inside device)
- Parent–Child relation → inheritance
- Different shapes drawing → polymorphism
Important Points
- Encapsulation → data + methods together
- Inheritance → code reuse
- Polymorphism → same function, different behavior
- Abstraction → complexity hide करना
Common mistakes
- concepts को mix कर देना
- inheritance का syntax गलत लिखना
- polymorphism को समझ न पाना
Mini Program
class Shape:
def area(self):
print("Area")
class Circle(Shape):
def area(self):
print("Circle Area")
c = Circle()
c.area()
Summary
Basic OOP concepts Python programming को advanced level तक ले जाते हैं।
इनकी मदद से code reusable, flexible और easy to manage बनता है।