Inheritance and Polymorphism in Python with Example
#Inheritance Polymorphism in Python with Example
Introduction
When building scalable and reusable applications in Python, object-oriented programming (OOP) concepts like inheritance and polymorphism play a crucial role. These principles make code more maintainable, reusable, and easier to extend.
In this guide, we’ll cover:
What is inheritance in Python?
How polymorphism works in Python.
Practical examples with code.
Best practices for developers.
Inheritance is an OOP concept where a class (child class) can acquire properties and behaviors (methods) from another class (parent class).
This allows you to:
Reuse existing code.
Avoid duplication.
Create a hierarchical class structure.
# Parent class
class Animal:
def speak(self):
return "This is an animal sound"
# Child class
class Dog(Animal):
def speak(self):
return "Woof! Woof!"
# Another Child class
class Cat(Animal):
def speak(self):
return "Meow!"
# Creating objects
dog = Dog()
cat = Cat()
print(dog.speak()) # Output: Woof! Woof!
print(cat.speak()) # Output: Meow!
✅ Here, Dog
and Cat
inherit from Animal
but override the speak()
method with their own behavior.
Polymorphism means “many forms.” In OOP, it refers to the ability of different classes to define methods with the same name but different behaviors.
In Python, polymorphism is often achieved through method overriding or duck typing.
class Bird:
def fly(self):
return "Bird is flying high in the sky"
class Airplane:
def fly(self):
return "Airplane is flying through the clouds"
# Polymorphism in action
for obj in [Bird(), Airplane()]:
print(obj.fly())
Output:
Bird is flying high in the sky
Airplane is flying through the clouds
✅ Both classes have a method fly()
, but their implementation is different. This is polymorphism in action.
Feature | Inheritance | Polymorphism |
---|---|---|
Definition | Mechanism of acquiring properties/methods from parent class | Ability of methods to take different forms |
Purpose | Code reusability, avoid duplication | Flexibility, method overriding, dynamic behavior |
Example | Dog inherits from Animal | Bird and Airplane both implement fly() differently |
Use inheritance only when there is a true “is-a” relationship.
Keep the class hierarchy simple and avoid deep inheritance chains.
Use method overriding for polymorphism but maintain meaningful method names.
Prefer composition over inheritance when possible for better code flexibility.
Both inheritance and polymorphism are foundational concepts of Python OOP. Inheritance helps in reusing code by deriving classes from existing ones, while polymorphism allows multiple classes to define methods with the same name but different behaviors.
By mastering these, you can write cleaner, scalable, and more Pythonic code for real-world applications.
"Learn inheritance and polymorphism in Python with examples. A complete guide to Python OOP concepts with code, method overriding, and best practices for developers."
inheritance in python, polymorphism in python, python inheritance example, python polymorphism example, oop concepts in python, method overriding python, python class inheritance, python object oriented programming