what is polymorphism in python with example

what is polymorphism :

In Python, Polymorphism lets us define methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class inherits the methods from the parent class. … This process of re-implementing a method in the child class is known as Method Overriding.

in simple word one class and many forms is that polymorphism.

# this is the example code of polymorphism.

class Cars():
    def audi(self):
        print("this is audi car.")
  
    def jaguar(self):
        print("this is jaguar car.")
  
    def ferrari(self):
        print("this is ferrari car.")
    
obj_car = Cars()
obj_car.audi()
obj_car.jaguar()
obj_car.ferrari()


basically two types of polymorphism :

runtime polymorphism :

runtime polymorphism is could method overriding. it means the class name is the same and the parameter is all so the same is could method overloading or runtime polymorphism.

compile time polymorphism:

compile-time polymorphism is a could method overloading. it means the class name is the same but the parameter is not the same is could method overwriting or compile-time polymorphism.

A real-world example of polymorphism :

The word polymorphism means having many forms. A real-life example of polymorphism: A person at the same time can have different characteristics. Like a woman at the same time is a mather, a wife, an employee. So the same person posses different behavior in different situations. This is called polymorphism.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on whatsapp
WhatsApp
Share on reddit
Reddit
Share on telegram
Telegram

Leave a Comment

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

Scroll to Top