|
| 1 | +### **Day 19: Introduction to Object-Oriented Programming (OOP)** |
| 2 | +Object-Oriented Programming is a programming paradigm that uses objects to represent real-world entities. |
| 3 | + |
| 4 | +- **Classes and Objects:** |
| 5 | + - Classes define blueprints for creating objects. |
| 6 | + - Objects are instances of classes. |
| 7 | +- **Attributes and Methods:** |
| 8 | + - Classes can have attributes (data) and methods (functions) that define their properties and behavior. |
| 9 | + |
| 10 | +**Example of creating a simple class and an object:** |
| 11 | +```python |
| 12 | +# Define a simple class |
| 13 | +class Dog: |
| 14 | + def __init__(self, name): |
| 15 | + self.name = name |
| 16 | + |
| 17 | + def bark(self): |
| 18 | + print(f"{self.name} says woof!") |
| 19 | + |
| 20 | +# Create an object (instance) of the Dog class |
| 21 | +my_dog = Dog("Buddy") |
| 22 | +my_dog.bark() # Call the bark method on the object |
| 23 | +``` |
| 24 | + |
| 25 | +### **Day 20: Class Attributes and Methods** |
| 26 | +Classes can have attributes and methods that define their behavior. |
| 27 | + |
| 28 | +- **Class Attributes:** |
| 29 | + - Class attributes are shared among all instances of the class. |
| 30 | +- **Instance Attributes:** |
| 31 | + - Instance attributes are specific to individual objects. |
| 32 | +- **Methods:** |
| 33 | + - Methods are functions defined within a class. |
| 34 | + |
| 35 | +**Example of class attributes and methods:** |
| 36 | +```python |
| 37 | +class Circle: |
| 38 | + def __init__(self, radius): |
| 39 | + self.radius = radius |
| 40 | + |
| 41 | + def area(self): |
| 42 | + return 3.14159 * self.radius**2 |
| 43 | + |
| 44 | + def circumference(self): |
| 45 | + return 2 * 3.14159 * self.radius |
| 46 | + |
| 47 | +my_circle = Circle(5) |
| 48 | +print(f"Area: {my_circle.area()}") |
| 49 | +print(f"Circumference: {my_circle.circumference()}") |
| 50 | +``` |
| 51 | + |
| 52 | +### **Day 21: Inheritance** |
| 53 | +Inheritance allows you to create new classes that inherit attributes and methods from existing classes. |
| 54 | + |
| 55 | +- **Parent Class (Superclass):** |
| 56 | + - The parent class defines common attributes and methods. |
| 57 | +- **Child Class (Subclass):** |
| 58 | + - The child class inherits from the parent class and can have additional attributes and methods. |
| 59 | + |
| 60 | +**Example of inheritance:** |
| 61 | +```python |
| 62 | +# Parent class |
| 63 | +class Animal: |
| 64 | + def __init__(self, name): |
| 65 | + self.name = name |
| 66 | + |
| 67 | + def speak(self): |
| 68 | + pass |
| 69 | + |
| 70 | +# Child class inheriting from Animal |
| 71 | +class Dog(Animal): |
| 72 | + def speak(self): |
| 73 | + return f"{self.name} says woof!" |
| 74 | + |
| 75 | +my_dog = Dog("Buddy") |
| 76 | +print(my_dog.speak()) # Calls the speak method of the Dog class |
| 77 | +``` |
| 78 | + |
| 79 | +### **Day 22: Polymorphism** |
| 80 | +Polymorphism allows objects of different classes to be treated as objects of a common superclass. |
| 81 | + |
| 82 | +- **Common Superclass:** |
| 83 | + - Create a common superclass that defines shared methods or attributes. |
| 84 | +- **Subclasses with Different Implementations:** |
| 85 | + - Subclasses provide their own implementations of methods. |
| 86 | + |
| 87 | +**Example of polymorphism:** |
| 88 | +```python |
| 89 | +# Common superclass |
| 90 | +class Shape: |
| 91 | + def area(self): |
| 92 | + pass |
| 93 | + |
| 94 | +# Subclasses with different implementations of area |
| 95 | +class Circle(Shape): |
| 96 | + def __init__(self, radius): |
| 97 | + self.radius = radius |
| 98 | + |
| 99 | + def area(self): |
| 100 | + return 3.14159 * self.radius**2 |
| 101 | + |
| 102 | +class Rectangle(Shape): |
| 103 | + def __init__(self, width, height): |
| 104 | + self.width = width |
| 105 | + self.height = height |
| 106 | + |
| 107 | + def area(self): |
| 108 | + return self.width * self.height |
| 109 | + |
| 110 | +shapes = [Circle(5), Rectangle(4, 6)] |
| 111 | + |
| 112 | +for shape in shapes: |
| 113 | + print(f"Area: {shape.area()}") |
| 114 | +``` |
| 115 | + |
| 116 | +Object-Oriented Programming (OOP) is a fundamental concept in Python and many other programming languages. It allows you to model real-world entities, promote code organization, and enhance code reusability. Practice with these examples to become proficient in using OOP principles in Python. |
0 commit comments