What Are Python Inheritance and Polymorphism? See Examples

Inheritance and polymorphism are core OOP concepts in Python:

  • Inheritance: Allows a class (child) to reuse properties and methods of another class (parent)
  • Polymorphism: Allows objects of different classes to use the same interface or method name, providing flexible behavior

These concepts promote code reuse, modularity, and scalability in real-world applications.


Why Inheritance and Polymorphism Are Important

  • Avoid code duplication
  • Make code modular and maintainable
  • Enable flexible and extendable designs
  • Used in web frameworks, software systems, and simulations

Example 1: Basic Inheritance

class Vehicle:
    def start(self):
        print("Vehicle started")

class Car(Vehicle):
    def honk(self):
        print("Car horn sounds!")

my_car = Car()
my_car.start()  # Inherited from Vehicle
my_car.honk()
  • Car inherits methods from Vehicle

Example 2: Polymorphism with Methods

class Cat:
    def sound(self):
        print("Meow")

class Dog:
    def sound(self):
        print("Woof")

animals = [Cat(), Dog()]
for animal in animals:
    animal.sound()

Output:

Meow
Woof
  • Same method sound() behaves differently based on object type

Example 3: Real-World Scenario – Employee Hierarchy

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def work(self):
        print(f"{self.name} is working")

class Manager(Employee):
    def work(self):
        print(f"{self.name} is managing the team")

class Developer(Employee):
    def work(self):
        print(f"{self.name} is writing code")

employees = [Manager("Alice", 90000), Developer("Bob", 70000)]
for emp in employees:
    emp.work()
  • Demonstrates inheritance and method overriding
  • Polymorphism allows calling work() uniformly on different objects

Example 4: Real-World Scenario – Shape Area Calculation

class Shape:
    def area(self):
        pass

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        import math
        return math.pi * self.radius ** 2

shapes = [Rectangle(10, 5), Circle(7)]
for shape in shapes:
    print(f"Area: {shape.area()}")
  • Polymorphism allows different shapes to implement area() differently
  • Useful in geometry, CAD, graphics, and simulations

Best Practices

✔ Use inheritance to promote code reuse
✔ Avoid deep inheritance hierarchies for simplicity
✔ Use polymorphism to generalize method calls across classes
✔ Override methods carefully and document behavior


Conclusion

Python inheritance and polymorphism are essential for writing reusable, modular, and scalable object-oriented code. Mastering these concepts allows developers to design flexible and maintainable applications in real-world scenarios like employee systems, shapes, vehicles, and simulations.


References

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *