|
| 1 | +# Creating a Class: |
| 2 | +# Define a simple Python class. |
| 3 | +class Dog: |
| 4 | + def __init__(self, name, breed): |
| 5 | + self.name = name |
| 6 | + self.breed = breed |
| 7 | + |
| 8 | + def bark(self): |
| 9 | + return "Woof!" |
| 10 | + |
| 11 | +# Inheritance: |
| 12 | +# Create a subclass that inherits from a parent class. |
| 13 | +class Animal: |
| 14 | + def speak(self): |
| 15 | + pass |
| 16 | + |
| 17 | +class Dog(Animal): |
| 18 | + def speak(self): |
| 19 | + return "Woof!" |
| 20 | + |
| 21 | +class Cat(Animal): |
| 22 | + def speak(self): |
| 23 | + return "Meow!" |
| 24 | + |
| 25 | +# Encapsulation: |
| 26 | +# Implement encapsulation by using private and public attributes. |
| 27 | +class Circle: |
| 28 | + def __init__(self, radius): |
| 29 | + self.__radius = radius # Private attribute |
| 30 | + |
| 31 | + def area(self): |
| 32 | + return 3.14 * self.__radius * self.__radius |
| 33 | + |
| 34 | +# Polymorphism: |
| 35 | +# Demonstrate polymorphism with a common method name. |
| 36 | +class Animal: |
| 37 | + def speak(self): |
| 38 | + pass |
| 39 | + |
| 40 | +class Dog(Animal): |
| 41 | + def speak(self): |
| 42 | + return "Woof!" |
| 43 | + |
| 44 | +class Cat(Animal): |
| 45 | + def speak(self): |
| 46 | + return "Meow!" |
| 47 | + |
| 48 | +def animal_sound(animal): |
| 49 | + return animal.speak() |
| 50 | + |
| 51 | +# Abstraction: |
| 52 | +# Implement abstraction by defining an abstract base class. |
| 53 | +from abc import ABC, abstractmethod |
| 54 | + |
| 55 | +class Shape(ABC): |
| 56 | + @abstractmethod |
| 57 | + def area(self): |
| 58 | + pass |
| 59 | + |
| 60 | +class Circle(Shape): |
| 61 | + def __init__(self, radius): |
| 62 | + self.radius = radius |
| 63 | + |
| 64 | + def area(self): |
| 65 | + return 3.14 * self.radius * self.radius |
| 66 | + |
| 67 | +# Getter and Setter Methods: |
| 68 | +# Use getter and setter methods for attribute access. |
| 69 | +class Person: |
| 70 | + def __init__(self, name, age): |
| 71 | + self._name = name |
| 72 | + |
| 73 | + def get_name(self): |
| 74 | + return self._name |
| 75 | + |
| 76 | + def set_name(self, name): |
| 77 | + self._name = name |
| 78 | + |
| 79 | +# Composition: |
| 80 | +# Create a class that is composed of other classes. |
| 81 | +class Engine: |
| 82 | + def start(self): |
| 83 | + return "Engine started." |
| 84 | + |
| 85 | +class Car: |
| 86 | + def __init__(self): |
| 87 | + self.engine = Engine() |
| 88 | + |
| 89 | + def start(self): |
| 90 | + return self.engine.start() |
| 91 | + |
| 92 | +# Method Overloading: |
| 93 | +# Implement method overloading using default arguments. |
| 94 | +class Calculator: |
| 95 | + def add(self, a, b=0): |
| 96 | + return a + b |
| 97 | + |
| 98 | +# Class Variables: |
| 99 | +# Define and use class-level variables. |
| 100 | +class Student: |
| 101 | + count = 0 |
| 102 | + |
| 103 | + def __init__(self, name): |
| 104 | + self.name = name |
| 105 | + Student.count += 1 |
| 106 | + |
| 107 | +# Multiple Inheritance: |
| 108 | +# Create a class that inherits from multiple parent classes. |
| 109 | +class A: |
| 110 | + def method_a(self): |
| 111 | + return "Method A" |
| 112 | + |
| 113 | +class B: |
| 114 | + def method_b(self): |
| 115 | + return "Method B" |
| 116 | + |
| 117 | +class C(A, B): |
| 118 | + pass |
0 commit comments