|
| 1 | +--- |
| 2 | +id: python-OOPs |
| 3 | +title: Python OOPs concept |
| 4 | +sidebar_label: Python OOPs concept #displays in sidebar |
| 5 | +sidebar_position: 19 |
| 6 | +author: Seema Kumari Patel |
| 7 | +description: OOPs concept in Python |
| 8 | +tags: |
| 9 | + [ |
| 10 | + Python, |
| 11 | + Introduction of python, |
| 12 | + Python Syntax, |
| 13 | + Python Variables, |
| 14 | + Python Operators, |
| 15 | + ] |
| 16 | +--- |
| 17 | + |
| 18 | + |
| 19 | +# Python OOPs |
| 20 | + |
| 21 | +**OOP** is a way of organizing code that uses objects and classes to represent real-world entities and their behavior. In OOP, object has attributes thing that has specific data and can perform certain actions using methods. |
| 22 | + |
| 23 | + |
| 24 | +Characteristics of OOP (Object Oriented Programming) |
| 25 | +Python supports the core principles of object-oriented programming, which are the building blocks for designing robust and reusable software. The diagram below demonstrates these core principles: |
| 26 | + |
| 27 | +1. Class - A blueprint for creating objects. |
| 28 | + |
| 29 | +📌 **Use Case**: create a class - car |
| 30 | + |
| 31 | +```python |
| 32 | +class Car: |
| 33 | + def __init__(self, brand, model): |
| 34 | + self.brand = brand |
| 35 | + self.model = model |
| 36 | + |
| 37 | + def start(self): |
| 38 | + print(f"{self.brand} {self.model} is starting...") |
| 39 | +``` |
| 40 | + |
| 41 | +2. Object - An instance of a class. |
| 42 | + |
| 43 | +📌 **Use Case**: instantiate the variables |
| 44 | + |
| 45 | +```python |
| 46 | +my_car = Car("Tesla", "Model S") |
| 47 | +my_car.start() # Tesla Model S is starting... |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | +3. Encapsulation - Hiding the internal details and only exposing necessary parts. |
| 52 | + |
| 53 | +📌 **Use Case**: Not letting data to be accessed by other class |
| 54 | + |
| 55 | +```python |
| 56 | +class BankAccount: |
| 57 | + def __init__(self, balance): |
| 58 | + self.__balance = balance # private variable |
| 59 | + |
| 60 | + def deposit(self, amount): |
| 61 | + self.__balance += amount |
| 62 | + |
| 63 | + def get_balance(self): |
| 64 | + return self.__balance |
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | +4. Inheritance - One class can inherit from another. |
| 69 | + |
| 70 | +📌 **Use Case**: car (parent) class is getting inherited by (child) ElectricCar |
| 71 | + |
| 72 | +```python |
| 73 | +class ElectricCar(Car): |
| 74 | + def __init__(self, brand, model, battery): |
| 75 | + super().__init__(brand, model) |
| 76 | + self.battery = battery |
| 77 | + |
| 78 | + def battery_info(self): |
| 79 | + print(f"Battery: {self.battery} kWh") |
| 80 | + |
| 81 | +tesla = ElectricCar("Tesla", "Model X", 100) |
| 82 | +tesla.start() |
| 83 | +tesla.battery_info() |
| 84 | +``` |
| 85 | + |
| 86 | + |
| 87 | +5. Polymorphism - Same function name, but different behavior depending on the object. |
| 88 | + |
| 89 | +📌 **Use Case**: Different classes using single method for different purposes. |
| 90 | + |
| 91 | +```python |
| 92 | +class Dog: |
| 93 | + def speak(self): |
| 94 | + return "Woof!" |
| 95 | + |
| 96 | +class Cat: |
| 97 | + def speak(self): |
| 98 | + return "Meow!" |
| 99 | + |
| 100 | +pets = [Dog(), Cat()] |
| 101 | +for pet in pets: |
| 102 | + print(pet.speak()) # Woof! / Meow! |
| 103 | +``` |
| 104 | + |
| 105 | + |
| 106 | +6. Abstraction - Hiding implementation details, showing only essential features (using abc module). |
| 107 | + |
| 108 | +📌 **Use Case**: Hiding low level details |
| 109 | + |
| 110 | +```python |
| 111 | +from abc import ABC, abstractmethod |
| 112 | + |
| 113 | +class Shape(ABC): |
| 114 | + @abstractmethod |
| 115 | + def area(self): |
| 116 | + pass |
| 117 | + |
| 118 | +class Circle(Shape): |
| 119 | + def __init__(self, radius): |
| 120 | + self.radius = radius |
| 121 | + |
| 122 | + def area(self): |
| 123 | + return 3.14 * self.radius ** 2 |
| 124 | + |
| 125 | +c = Circle(5) |
| 126 | +print(c.area()) # 78.5 |
| 127 | +``` |
| 128 | + |
| 129 | + |
0 commit comments