Object-Oriented Programming (OOP) in Python
Object-Oriented Programming (OOP) is a method of structuring a program by bundling related properties and behaviors into individual objects.
Object-Oriented Programming (OOP) एक तरीका है जिसमें हम प्रोग्राम को objects में organize करते हैं, जिनमें properties और behaviors शामिल होते हैं।
Core OOP Concepts
- Class - Blueprint for creating objects.
- Object - Instance of a class.
- Inheritance - One class can inherit from another.
- Polymorphism - Same method behaves differently in different classes.
- Encapsulation - Hiding internal data from outside access.
- Abstraction - Hiding complex implementation details.
- Class - Object बनाने का खाका।
- Object - Class का instance।
- Inheritance - एक class दूसरी class से गुण ले सकती है।
- Polymorphism - एक method अलग-अलग classes में अलग तरह से behave करता है।
- Encapsulation - Internal डेटा को बाहर से छिपाना।
- Abstraction - Complex details को छुपाकर जरूरी चीजें दिखाना।
Example 1: Create Employee Class
class Employee:
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
e1 = Employee('Aryan', 25, 50000)
print(type(e1))
Output:
<class '__main__.Employee'>
This creates an Employee class with attributes and one object.
यह Employee class के साथ एक object बनाता है।
Example 2: Access Attributes
print(e1.name, e1.age, e1.salary)
Output:
Aryan 25 50000
Access and print employee attributes.
Employee की attributes को access और print किया गया।
Example 3: Add Method to Display Info
class Employee:
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
def show(self):
print(f'{self.name}, Age: {self.age}, Salary: {self.salary}')
e1 = Employee('Naincy', 28, 55000)
e1.show()
Output:
Naincy, Age: 28, Salary: 55000
show() method displays the employee data.
show() method employee की details दिखाता है।