|
| 1 | + |
| 2 | +In Python object-oriented Programming (OOPs) is a programming paradigm |
| 3 | +that uses objects and classes in programming. It aims to implement |
| 4 | +real-world entities like inheritance, polymorphisms, encapsulation, etc. |
| 5 | +in the programming. The main concept of object-oriented Programming |
| 6 | +(OOPs) or oops concepts in Python is to bind the data and the functions |
| 7 | +that work together as a single unit so that no other part of the code |
| 8 | +can access this data. |
| 9 | + |
| 10 | +**OOPs Concepts in Python** |
| 11 | + |
| 12 | +1. Class in Python |
| 13 | + |
| 14 | +2. Objects in Python |
| 15 | + |
| 16 | +3. Polymorphism in Python |
| 17 | + |
| 18 | +4. Encapsulation in Python |
| 19 | + |
| 20 | +5. Inheritance in Python |
| 21 | + |
| 22 | +6. Data Abstraction in Python |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +Python Class A class is a collection of objects. A class contains the |
| 27 | +blueprints or the prototype from which the objects are being created. It |
| 28 | +is a logical entity that contains some attributes and methods. |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +```python |
| 34 | +#Simple Class in Python |
| 35 | +class Dog: |
| 36 | + pass |
| 37 | +``` |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +**Python Objects** In object oriented programming Python, The object is |
| 43 | +an entity that has a state and behavior associated with it. It may be |
| 44 | +any real-world object like a mouse, keyboard, chair, table, pen, etc. |
| 45 | +Integers, strings, floating-point numbers, even arrays, and |
| 46 | +dictionaries, are all objects. |
| 47 | + |
| 48 | + |
| 49 | +```python |
| 50 | +obj = Dog() |
| 51 | +``` |
| 52 | +This creates an instance for class Dog |
| 53 | + |
| 54 | +**The Python **init** Method** |
| 55 | + |
| 56 | +The **init** method is similar to constructors in C++ and Java. It is |
| 57 | +run as soon as an object of a class is instantiated. The method is |
| 58 | +useful to do any initialization you want to do with your object. |
| 59 | + |
| 60 | + |
| 61 | +```python |
| 62 | +class Dog: |
| 63 | + |
| 64 | + # class attribute |
| 65 | + attr1 = "mammal" |
| 66 | + |
| 67 | + # Instance attribute |
| 68 | + def __init__(self, name): |
| 69 | + self.name = name |
| 70 | + |
| 71 | +# Object instantiation |
| 72 | +Rodger = Dog("Rodger") |
| 73 | +Tommy = Dog("Tommy") |
| 74 | + |
| 75 | +# Accessing class attributes |
| 76 | +print("Rodger is a {}".format(Rodger.__class__.attr1)) |
| 77 | +print("Tommy is also a {}".format(Tommy.__class__.attr1)) |
| 78 | + |
| 79 | +# Accessing instance attributes |
| 80 | +print("My name is {}".format(Rodger.name)) |
| 81 | +print("My name is {}".format(Tommy.name)) |
| 82 | +``` |
| 83 | +In the above mentioned code, init method is used to initialize the name. |
| 84 | + |
| 85 | +**Inheritance** |
| 86 | + |
| 87 | +In Python object oriented Programming, Inheritance is the capability of |
| 88 | +one class to derive or inherit the properties from another class. The |
| 89 | +class that derives properties is called the derived class or child class |
| 90 | +and the class from which the properties are being derived is called the |
| 91 | +base class or parent class. |
| 92 | + |
| 93 | +Types of Inheritances: |
| 94 | + |
| 95 | +- Single Inheritance |
| 96 | + |
| 97 | +- Multilevel Inheritance |
| 98 | + |
| 99 | +- Multiple Inheritance |
| 100 | + |
| 101 | +- Hierarchial Inheritance |
| 102 | + |
| 103 | +```python |
| 104 | +#Single Inheritance |
| 105 | +# Parent class |
| 106 | +class Animal: |
| 107 | + def __init__(self, name, sound): |
| 108 | + self.name = name |
| 109 | + self.sound = sound |
| 110 | + |
| 111 | + def make_sound(self): |
| 112 | + print(f"{self.name} says {self.sound}") |
| 113 | + |
| 114 | +# Child class inheriting from Animal |
| 115 | +class Dog(Animal): |
| 116 | + def __init__(self, name): |
| 117 | + # Call the constructor of the parent class |
| 118 | + super().__init__(name, "Woof") |
| 119 | + |
| 120 | +# Child class inheriting from Animal |
| 121 | +class Cat(Animal): |
| 122 | + def __init__(self, name): |
| 123 | + # Call the constructor of the parent class |
| 124 | + super().__init__(name, "Meow") |
| 125 | + |
| 126 | +# Creating objects of the derived classes |
| 127 | +dog = Dog("Buddy") |
| 128 | +cat = Cat("Whiskers") |
| 129 | + |
| 130 | +# Accessing methods of the parent class |
| 131 | +dog.make_sound() |
| 132 | +cat.make_sound() |
| 133 | +``` |
| 134 | +The above code depicts the Single Inheritance, in case of single inheritance there's only a single base class and a derived class. Here, Dog and Cat are the derived classes with Animal as the parent class. They can access the methods of the base class or derive their own methods. |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +```python |
| 139 | +#Multilevel Inheritance |
| 140 | +# Parent class |
| 141 | +class Animal: |
| 142 | + def __init__(self, name): |
| 143 | + self.name = name |
| 144 | + |
| 145 | + def speak(self): |
| 146 | + print(f"{self.name} speaks") |
| 147 | + |
| 148 | +# Child class inheriting from Animal |
| 149 | +class Dog(Animal): |
| 150 | + def bark(self): |
| 151 | + print(f"{self.name} barks") |
| 152 | + |
| 153 | +# Grandchild class inheriting from Dog |
| 154 | +class GermanShepherd(Dog): |
| 155 | + def guard(self): |
| 156 | + print(f"{self.name} guards") |
| 157 | + |
| 158 | +# Creating objects of the derived classes |
| 159 | +german_shepherd = GermanShepherd("Rocky") |
| 160 | + |
| 161 | +# Accessing methods from all levels of inheritance |
| 162 | +german_shepherd.speak() # Accessing method from the Animal class |
| 163 | +german_shepherd.bark() # Accessing method from the Dog class |
| 164 | +german_shepherd.guard() # Accessing method from the GermanShepherd class |
| 165 | +``` |
| 166 | +Multilevel inheritance is a concept in object-oriented programming where a class inherits properties and behaviors from another class, which itself may inherit from another class. In other words, it involves a chain of inheritance where a subclass inherits from a superclass, and that subclass can then become a superclass for another subclass.Its similar to GrandFather ,Father and Son .In the above code,Animal class is the superclass, Dog is derived from Animal and Dog is the parent of GermanShepherd. GermenShepherd is the child class of Dog. GermenShepherd can access methods of both Animal and Dog. |
| 167 | + |
| 168 | + |
| 169 | +```python |
| 170 | +#Hierarchial Inheritance |
| 171 | +# Parent class |
| 172 | +class Animal: |
| 173 | + def __init__(self, name): |
| 174 | + self.name = name |
| 175 | + |
| 176 | + def speak(self): |
| 177 | + print(f"{self.name} speaks") |
| 178 | + |
| 179 | +# Child class 1 inheriting from Animal |
| 180 | +class Dog(Animal): |
| 181 | + def bark(self): |
| 182 | + print(f"{self.name} barks") |
| 183 | + |
| 184 | +# Child class 2 inheriting from Animal |
| 185 | +class Cat(Animal): |
| 186 | + def meow(self): |
| 187 | + print(f"{self.name} meows") |
| 188 | + |
| 189 | +# Creating objects of the derived classes |
| 190 | +dog = Dog("Buddy") |
| 191 | +cat = Cat("Whiskers") |
| 192 | + |
| 193 | +# Accessing methods from the parent and child classes |
| 194 | +dog.speak() # Accessing method from the Animal class |
| 195 | +dog.bark() # Accessing method from the Dog class |
| 196 | +cat.speak() # Accessing method from the Animal class |
| 197 | +cat.meow() # Accessing method from the Cat class |
| 198 | +``` |
| 199 | +Hierarchical inheritance is a type of inheritance in object-oriented programming where one class serves as a superclass for multiple subclasses. In this inheritance model, each subclass inherits properties and behaviors from the same superclass, creating a hierarchical tree-like structure. |
| 200 | + |
| 201 | +```python |
| 202 | +#Multiple Inheritance |
| 203 | +# Parent class 1 |
| 204 | +class Herbivore: |
| 205 | + def eat_plants(self): |
| 206 | + print("Eating plants") |
| 207 | + |
| 208 | +# Parent class 2 |
| 209 | +class Carnivore: |
| 210 | + def eat_meat(self): |
| 211 | + print("Eating meat") |
| 212 | + |
| 213 | +# Child class inheriting from both Herbivore and Carnivore |
| 214 | +class Omnivore(Herbivore, Carnivore): |
| 215 | + def eat(self): |
| 216 | + print("Eating everything") |
| 217 | + |
| 218 | +# Creating an object of the Omnivore class |
| 219 | +omnivore = Omnivore() |
| 220 | + |
| 221 | +# Accessing methods from both parent classes |
| 222 | +omnivore.eat_plants() # Accessing method from Herbivore |
| 223 | +omnivore.eat_meat() # Accessing method from Carnivore |
| 224 | +omnivore.eat() # Accessing method from Omnivore |
| 225 | +``` |
| 226 | +Multiple inheritance is a concept in object-oriented programming where a class can inherit properties and behaviors from more than one parent class. This means that a subclass can have multiple immediate parent classes, allowing it to inherit features from each of them. |
| 227 | + |
| 228 | +**Polymorphism** In object oriented Programming Python, Polymorphism |
| 229 | +simply means having many forms |
| 230 | + |
| 231 | +```python |
| 232 | +class Bird: |
| 233 | + |
| 234 | + def intro(self): |
| 235 | + print("There are many types of birds.") |
| 236 | + |
| 237 | + def flight(self): |
| 238 | + print("Most of the birds can fly but some cannot.") |
| 239 | + |
| 240 | +class sparrow(Bird): |
| 241 | + |
| 242 | + def flight(self): |
| 243 | + print("Sparrows can fly.") |
| 244 | + |
| 245 | +class ostrich(Bird): |
| 246 | + |
| 247 | + def flight(self): |
| 248 | + print("Ostriches cannot fly.") |
| 249 | + |
| 250 | +obj_bird = Bird() |
| 251 | +obj_spr = sparrow() |
| 252 | +obj_ost = ostrich() |
| 253 | + |
| 254 | +obj_bird.intro() |
| 255 | +obj_bird.flight() |
| 256 | + |
| 257 | +obj_spr.intro() |
| 258 | +obj_spr.flight() |
| 259 | + |
| 260 | +obj_ost.intro() |
| 261 | +obj_ost.flight() |
| 262 | +``` |
| 263 | +Poly stands for 'many' and morphism for 'forms'. In the above code, method flight() has many forms. |
| 264 | + |
| 265 | +**Python Encapsulation** |
| 266 | + |
| 267 | +In Python object oriented programming, Encapsulation is one of the |
| 268 | +fundamental concepts in object-oriented programming (OOP). It describes |
| 269 | +the idea of wrapping data and the methods that work on data within one |
| 270 | +unit. This puts restrictions on accessing variables and methods directly |
| 271 | +and can prevent the accidental modification of data. To prevent |
| 272 | +accidental change, an object's variable can only be changed by an |
| 273 | +object's method. Those types of variables are known as private |
| 274 | +variables. |
| 275 | + |
| 276 | + |
| 277 | +```python |
| 278 | +class Car: |
| 279 | + def __init__(self, make, model, year): |
| 280 | + self._make = make # Encapsulated attribute with single underscore |
| 281 | + self._model = model # Encapsulated attribute with single underscore |
| 282 | + self._year = year # Encapsulated attribute with single underscore |
| 283 | + self._odometer_reading = 0 # Encapsulated attribute with single underscore |
| 284 | + |
| 285 | + def get_make(self): |
| 286 | + return self._make |
| 287 | + |
| 288 | + def get_model(self): |
| 289 | + return self._model |
| 290 | + |
| 291 | + def get_year(self): |
| 292 | + return self._year |
| 293 | + |
| 294 | + def get_odometer_reading(self): |
| 295 | + return self._odometer_reading |
| 296 | + |
| 297 | + def update_odometer(self, mileage): |
| 298 | + if mileage >= self._odometer_reading: |
| 299 | + self._odometer_reading = mileage |
| 300 | + else: |
| 301 | + print("You can't roll back an odometer!") |
| 302 | + |
| 303 | + def increment_odometer(self, miles): |
| 304 | + self._odometer_reading += miles |
| 305 | + |
| 306 | +# Creating an instance of the Car class |
| 307 | +my_car = Car("Toyota", "Camry", 2021) |
| 308 | + |
| 309 | +# Accessing encapsulated attributes through methods |
| 310 | +print("Make:", my_car.get_make()) |
| 311 | +print("Model:", my_car.get_model()) |
| 312 | +print("Year:", my_car.get_year()) |
| 313 | + |
| 314 | +# Modifying encapsulated attribute through method |
| 315 | +my_car.update_odometer(100) |
| 316 | +print("Odometer Reading:", my_car.get_odometer_reading()) |
| 317 | + |
| 318 | +# Incrementing odometer reading |
| 319 | +my_car.increment_odometer(50) |
| 320 | +print("Odometer Reading after increment:", my_car.get_odometer_reading()) |
| 321 | +``` |
| 322 | + |
| 323 | + |
| 324 | +**Data Abstraction** It hides unnecessary code details from the user. |
| 325 | +Also, when we do not want to give out sensitive parts of our code |
| 326 | +implementation and this is where data abstraction came. |
| 327 | + |
| 328 | +```python |
| 329 | +from abc import ABC, abstractmethod |
| 330 | + |
| 331 | +# Abstract class defining the interface for a Shape |
| 332 | +class Shape(ABC): |
| 333 | + def __init__(self, name): |
| 334 | + self.name = name |
| 335 | + |
| 336 | + @abstractmethod |
| 337 | + def area(self): |
| 338 | + pass |
| 339 | + |
| 340 | + @abstractmethod |
| 341 | + def perimeter(self): |
| 342 | + pass |
| 343 | + |
| 344 | +# Concrete class implementing the Shape interface for a Rectangle |
| 345 | +class Rectangle(Shape): |
| 346 | + def __init__(self, name, length, width): |
| 347 | + super().__init__(name) |
| 348 | + self.length = length |
| 349 | + self.width = width |
| 350 | + |
| 351 | + def area(self): |
| 352 | + return self.length * self.width |
| 353 | + |
| 354 | + def perimeter(self): |
| 355 | + return 2 * (self.length + self.width) |
| 356 | + |
| 357 | +# Concrete class implementing the Shape interface for a Circle |
| 358 | +class Circle(Shape): |
| 359 | + def __init__(self, name, radius): |
| 360 | + super().__init__(name) |
| 361 | + self.radius = radius |
| 362 | + |
| 363 | + def area(self): |
| 364 | + return 3.14 * self.radius * self.radius |
| 365 | + |
| 366 | + def perimeter(self): |
| 367 | + return 2 * 3.14 * self.radius |
| 368 | + |
| 369 | +# Creating objects of the derived classes |
| 370 | +rectangle = Rectangle("Rectangle", 5, 4) |
| 371 | +circle = Circle("Circle", 3) |
| 372 | + |
| 373 | +# Accessing methods defined by the Shape interface |
| 374 | +print(f"{rectangle.name}: Area = {rectangle.area()}, Perimeter = {rectangle.perimeter()}") |
| 375 | +print(f"{circle.name}: Area = {circle.area()}, Perimeter = {circle.perimeter()}") |
| 376 | +``` |
| 377 | +To implement Data Abstraction , we have to import abc . ABC stands for Abstract Base Class . All those classes which want to implement data abstraction have to inherit from ABC. |
| 378 | +@abstractmethod is a decorator provided by the abc module, which stands for "abstract method". It's used to define abstract methods within abstract base classes (ABCs). An abstract method is a method declared in a class, but it does not contain an implementation. Instead, it serves as a placeholder, and its concrete implementation must be provided by subclasses. |
| 379 | +Abstract methods can be implemented by the derived classes. |
0 commit comments