Python supports Object-Oriented Programming (OOP), which allows structuring programs using classes, objects, attributes, and principles like inheritance, overloading, overriding, and polymorphism.
🔹 1️⃣ Classes and Objects
➤ What is a Class?
A class is a blueprint for creating objects. It defines attributes (variables) and methods (functions) that describe the behavior of objects.
➤ What is an Object?
An object is an instance of a class. It has its own values for the attributes and can use the class methods.
🔹 Example: Creating a Class and an Object
class Car:
def __init__(self, brand, model, year): # Constructor
self.brand = brand
self.model = model
self.year = year
def display_info(self): # Method
print(f"{self.year} {self.brand} {self.model}")
# Creating an object (instance)
my_car = Car("Toyota", "Corolla", 2022)
my_car.display_info() # Output: 2022 Toyota Corolla
🔹 2️⃣ Class and Instance Attributes
- Class Attributes → Shared by all instances of the class.
- Instance Attributes → Unique to each object.
Example: Class vs. Instance Attributes
class Student:
school = "ABC High School" # Class attribute (shared)
def __init__(self, name, grade):
self.name = name # Instance attribute
self.grade = grade # Instance attribute
student1 = Student("Alice", "A")
student2 = Student("Bob", "B")
print(student1.school) # Output: ABC High School
print(student2.school) # Output: ABC High School
Student.school = "XYZ Academy" # Changing class attribute
print(student1.school) # Output: XYZ Academy
print(student2.school) # Output: XYZ Academy
🔹 3️⃣ Inheritance (Code Reusability)
Inheritance allows a class (child) to inherit properties and methods from another class (parent).
Example: Single Inheritance
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Animal makes a sound"
class Dog(Animal): # Inheriting from Animal class
def speak(self): # Method Overriding
return "Woof! Woof!"
dog = Dog("Buddy")
print(dog.name) # Output: Buddy
print(dog.speak()) # Output: Woof! Woof!
Example: Multiple Inheritance
class A:
def method_a(self):
return "Method from A"
class B:
def method_b(self):
return "Method from B"
class C(A, B): # Inheriting from both A and B
pass
obj = C()
print(obj.method_a()) # Output: Method from A
print(obj.method_b()) # Output: Method from B
🔹 4️⃣ Method Overloading (Multiple Methods with the Same Name)
Python does not support true method overloading like Java or C++, but it can be simulated using default arguments.
Example: Simulating Method Overloading
class Math:
def add(self, a, b, c=0): # Default argument for overloading
return a + b + c
math = Math()
print(math.add(2, 3)) # Output: 5
print(math.add(2, 3, 4)) # Output: 9
🔹 5️⃣ Method Overriding (Redefining Methods in Child Class)
If a child class provides a specific implementation of a method already defined in its parent class, it overrides the parent method.
Example: Method Overriding
class Parent:
def show(self):
print("This is Parent class")
class Child(Parent):
def show(self):
print("This is Child class")
obj = Child()
obj.show() # Output: This is Child class
🔹 6️⃣ Polymorphism (Multiple Forms)
Polymorphism allows the same method to have different implementations based on the object calling it.
Example: Polymorphism in Functions
class Cat:
def speak(self):
return "Meow"
class Dog:
def speak(self):
return "Woof"
# Using polymorphism
def animal_speak(animal):
print(animal.speak())
cat = Cat()
dog = Dog()
animal_speak(cat) # Output: Meow
animal_speak(dog) # Output: Woof
Example: Polymorphism in Inheritance
class Shape:
def area(self):
pass # Abstract method
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
shapes = [Circle(5), Rectangle(4, 6)]
for shape in shapes:
print(shape.area()) # Calls the correct area method based on the object
✅ Output:
78.5 # Area of Circle
24 # Area of Rectangle
🔹 Summary Table
Concept | Description | Example |
---|---|---|
Class & Object | Blueprint for creating objects. | class Car → Car("Toyota", "Corolla", 2022) |
Attributes | Variables inside a class. | self.brand , self.model |
Inheritance | Child class inherits from Parent. | class Dog(Animal) |
Method Overloading | Simulating multiple methods with default arguments. | def add(self, a, b, c=0) |
Method Overriding | Child class redefines a method from Parent. | def speak(self): return "Woof" |
Polymorphism | Same method behaving differently based on the object. | def area(self): pass in Shape , overridden in Circle and Rectangle |
🔥 Key Takeaways
✅ Encapsulation – Organizes code into classes and objects.
✅ Inheritance – Promotes code reuse.
✅ Polymorphism – Enables flexibility in method calls.
✅ Overloading & Overriding – Allows method customization.
Would you like real-world examples like building a Banking System using OOP? 🚀
Leave a Reply