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