|
| 1 | +--- |
| 2 | +id: python-oops |
| 3 | +title: Object-Oriented Programming |
| 4 | +sidebar_label: OOPs in Python |
| 5 | +sidebar_position: 11 |
| 6 | +tags: |
| 7 | + [ |
| 8 | + Python, |
| 9 | + List in Python, |
| 10 | + Introduction of python, |
| 11 | + Python Syntax, |
| 12 | + Variables, |
| 13 | + Operators, |
| 14 | + Type Casting, |
| 15 | + String, |
| 16 | + Tuple in Python |
| 17 | + Array in Python |
| 18 | + Functions in Python |
| 19 | + Recursion in Python |
| 20 | + ] |
| 21 | +--- |
| 22 | + |
| 23 | +# Object-Oriented Programming (OOPs) in Python |
| 24 | + |
| 25 | +Python is an **object-oriented programming language**, which means it supports concepts like **classes, objects, inheritance, polymorphism, encapsulation, and abstraction**. |
| 26 | +OOP allows developers to structure programs so that properties and behaviors are bundled into objects, making code **modular, reusable, and easier to maintain**. |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## 🔹 What is OOP? |
| 31 | + |
| 32 | +- **Object-Oriented Programming (OOP)** is a programming paradigm based on the concept of **objects**. |
| 33 | +- Each object can hold **data (attributes)** and **functions (methods)** that operate on that data. |
| 34 | + |
| 35 | + **Benefits of OOP in Python:** |
| 36 | +- Reusability of code |
| 37 | +- Encapsulation of data |
| 38 | +- Modularity and abstraction |
| 39 | +- Easier debugging and maintenance |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## 🔹 Classes and Objects |
| 44 | + |
| 45 | +A **class** is a blueprint for creating objects. |
| 46 | +An **object** is an instance of a class. |
| 47 | + |
| 48 | +```python |
| 49 | +# Defining a class |
| 50 | +class Person: |
| 51 | + def __init__(self, name, age): |
| 52 | + self.name = name |
| 53 | + self.age = age |
| 54 | + |
| 55 | + def greet(self): |
| 56 | + return f"Hello, my name is {self.name} and I am {self.age} years old." |
| 57 | + |
| 58 | +# Creating objects |
| 59 | +p1 = Person("Alice", 25) |
| 60 | +p2 = Person("Bob", 30) |
| 61 | + |
| 62 | +print(p1.greet()) # Output: Hello, my name is Alice and I am 25 years old. |
| 63 | +print(p2.greet()) # Output: Hello, my name is Bob and I am 30 years old. |
| 64 | +```` |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +## 🔹 Attributes and Methods |
| 69 | + |
| 70 | +* **Attributes** → Variables inside a class. |
| 71 | +* **Methods** → Functions defined inside a class. |
| 72 | + |
| 73 | +````python |
| 74 | +class Car: |
| 75 | + wheels = 4 # Class attribute |
| 76 | + |
| 77 | + def __init__(self, brand, model): |
| 78 | + self.brand = brand # Instance attribute |
| 79 | + self.model = model |
| 80 | + |
| 81 | + def info(self): # Instance method |
| 82 | + return f"{self.brand} {self.model} has {Car.wheels} wheels." |
| 83 | + |
| 84 | +c1 = Car("Tesla", "Model S") |
| 85 | +print(c1.info()) # Tesla Model S has 4 wheels. |
| 86 | +```` |
| 87 | + |
| 88 | +## 🔹 Encapsulation |
| 89 | + |
| 90 | +Encapsulation means **restricting access** to certain variables/methods. |
| 91 | +In Python, we use: |
| 92 | + |
| 93 | +* `_protected_var` → convention for protected members |
| 94 | +* `__private_var` → name mangling for private members |
| 95 | + |
| 96 | +````python |
| 97 | +class BankAccount: |
| 98 | + def __init__(self, balance): |
| 99 | + self.__balance = balance # Private variable |
| 100 | + |
| 101 | + def deposit(self, amount): |
| 102 | + self.__balance += amount |
| 103 | + |
| 104 | + def get_balance(self): |
| 105 | + return self.__balance |
| 106 | + |
| 107 | +acc = BankAccount(1000) |
| 108 | +acc.deposit(500) |
| 109 | +print(acc.get_balance()) # 1500 |
| 110 | +```` |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## 🔹 Inheritance |
| 115 | + |
| 116 | +Inheritance allows a class (child) to acquire properties of another class (parent). |
| 117 | + |
| 118 | +````python |
| 119 | +# Base class |
| 120 | +class Animal: |
| 121 | + def speak(self): |
| 122 | + return "This is an animal." |
| 123 | + |
| 124 | +# Derived class |
| 125 | +class Dog(Animal): |
| 126 | + def speak(self): |
| 127 | + return "Woof! Woof!" |
| 128 | + |
| 129 | +class Cat(Animal): |
| 130 | + def speak(self): |
| 131 | + return "Meow!" |
| 132 | + |
| 133 | +d = Dog() |
| 134 | +c = Cat() |
| 135 | +print(d.speak()) # Woof! Woof! |
| 136 | +print(c.speak()) # Meow! |
| 137 | +```` |
| 138 | + |
| 139 | + |
| 140 | +## 🔹 Polymorphism |
| 141 | + |
| 142 | +Polymorphism allows the same method name to perform different tasks depending on the object. |
| 143 | + |
| 144 | +````python |
| 145 | +for animal in [Dog(), Cat()]: |
| 146 | + print(animal.speak()) |
| 147 | + |
| 148 | +# Output: |
| 149 | +# Woof! Woof! |
| 150 | +# Meow! |
| 151 | +```` |
| 152 | + |
| 153 | + |
| 154 | +## 🔹 Abstraction |
| 155 | + |
| 156 | +Abstraction means **hiding implementation details** and showing only the necessary functionality. |
| 157 | +In Python, we use the `abc` module to create abstract classes. |
| 158 | + |
| 159 | +````python |
| 160 | +from abc import ABC, abstractmethod |
| 161 | + |
| 162 | +class Shape(ABC): |
| 163 | + @abstractmethod |
| 164 | + def area(self): |
| 165 | + pass |
| 166 | + |
| 167 | +class Circle(Shape): |
| 168 | + def __init__(self, radius): |
| 169 | + self.radius = radius |
| 170 | + |
| 171 | + def area(self): |
| 172 | + return 3.14 * self.radius * self.radius |
| 173 | + |
| 174 | +c = Circle(5) |
| 175 | +print(c.area()) # 78.5 |
| 176 | +```` |
| 177 | + |
| 178 | +## 🔹 Summary |
| 179 | + |
| 180 | +* **Class & Object** → Blueprint and instance |
| 181 | +* **Attributes & Methods** → Data and behavior inside a class |
| 182 | +* **Encapsulation** → Restricting access |
| 183 | +* **Inheritance** → Reusing parent class features |
| 184 | +* **Polymorphism** → Same function name, different behavior |
| 185 | +* **Abstraction** → Hiding unnecessary details |
0 commit comments