What is Polymorphism in OOPs programming?

Related Courses

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Polymorphism in OOPs Programming

Introduction: Polymorphism is an important concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It enables a single interface to represent different types, allowing flexibility and code reusability. With polymorphism, an object can exhibit different behaviors depending on the context in which it is used. Python, being a powerful and versatile language, supports polymorphism effortlessly through various mechanisms.

What is Polymorphism?

Polymorphism allows objects to be processed in multiple ways depending on their class. It can be defined as the ability of a function, object, or method to take multiple forms. In Python, polymorphism is implemented through function overloading, function overriding, and virtual functions.

Polymorphism in Python

Python, being an OOP-supported language, provides various ways to implement polymorphism. Some of the key techniques include:

  • Function Overloading

  • Method Overriding

  • Using Polymorphic Functions and Objects

  • Class Methods

  • Inheritance-based Polymorphism

Built-in Polymorphic Functions in Python

Python includes several built-in functions that exhibit polymorphism. For example, the len() function works on different data types:

# Using len() for a string
print(len("Hello Python"))  # Output: 12

# Using len() for a list
print(len([10, 20, 30]))  # Output: 3

User-defined Polymorphic Functions

Polymorphism can also be achieved using user-defined functions.

# Function demonstrating Polymorphism
def add(x, y, z=0):  
    return x + y + z  

# Driver code  
print(add(2, 3))  # Output: 5
print(add(2, 3, 4))  # Output: 9

Polymorphism with Functions and Objects

Polymorphism can be implemented by defining a function that can take any object as an argument.

class Tomato:
    def type(self):
        print("Vegetable")
    def color(self):
        print("Red")

class Apple:
    def type(self):
        print("Fruit")
    def color(self):
        print("Red")
      
def func(obj):
    obj.type()
    obj.color()

obj_tomato = Tomato()
obj_apple = Apple()
func(obj_tomato)  # Output: Vegetable, Red
func(obj_apple)   # Output: Fruit, Red

Polymorphism with Class Methods

Polymorphism can be achieved in Python by defining class methods that share the same name but belong to different classes.

class India:
    def capital(self):
        print("New Delhi is the capital of India.")
    def language(self):
        print("Hindi is the most widely spoken language of India.")
    def type(self):
        print("India is a developing country.")

obj_ind = India()
obj_ind.capital()  # Output: New Delhi is the capital of India.
obj_ind.language() # Output: Hindi is the most widely spoken language of India.
obj_ind.type()     # Output: India is a developing country.

Polymorphism with Inheritance

Inheritance-based polymorphism occurs when a child class overrides a method from its parent class.

class Bird:
    def intro(self):
        print("There are many types of birds.")
    def flight(self):
        print("Most birds can fly, but some cannot.")
    
class Sparrow(Bird):
    def flight(self):
        print("Sparrows can fly.")
    
class Ostrich(Bird):
    def flight(self):
        print("Ostriches cannot fly.")
    
obj_bird = Bird()
obj_spr = Sparrow()
obj_ost = Ostrich()

obj_bird.intro()
obj_bird.flight()  # Output: Most birds can fly, but some cannot.

obj_spr.intro()
obj_spr.flight()   # Output: Sparrows can fly.

obj_ost.intro()
obj_ost.flight()   # Output: Ostriches cannot fly.

Conclusion

Polymorphism is a key concept in object-oriented programming that allows for flexibility, scalability, and code reusability. Python provides built-in polymorphic functions and allows users to implement polymorphism using function overloading, method overriding, and inheritance. Understanding and effectively using polymorphism can enhance the structure and efficiency of your Python programs.

Scope @ NareshIT

At NareshIT’s Python Application Development program, you will receive extensive hands-on training in front-end, middleware, and back-end technologies. Our curriculum includes real-world projects, industry insights, and expert-led training to ensure relevance and job readiness.