Python Programming (136 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (83 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

What is Polymorphism in OOPs programming?

Last updated on Aug 11,2023 106K Views

3 / 5 Blog from Python OOPs

Polymorphism defines the ability to take different forms. Polymorphism in Python allows us to define methods in the child class with the same name as defined in their parent class. In this article, we will get into the details of Polymorphism in Python in the following sequence:

Check out the  Python Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Python Certification Training will help you gain expertise in Quantitative Analysis, data mining, and the presentation of data to see beyond the numbers by transforming your career into Data Scientist role.

What is Polymorphism?

Polymorphism is taken from the Greek words Poly (many) and morphism (forms). It means that the same function name can be used for different types. This makes programming more intuitive and easier.

In Python, we have different ways to define polymorphism. So let’s move ahead and see how polymorphism works in Python.

Polymorphism in Python

A child class inherits all the methods from the parent class. However, in some situations, the method inherited from the parent class doesn’t quite fit into the child class. In such cases, you will have to re-implement method in the child class.

Polymorphism- polymorphism in python-Edureka

There are different methods to use polymorphism in Python. You can use different function, class methods or objects to define polymorphism. So, let’s move ahead and have a look at each of these methods in detail.

Polymorphism with Function and Objects

You can create a function that can take any object, allowing for polymorphism.

Let’s take an example and create a function called “func()” which will take an object which we will name “obj”. Now, let’s give the function something to do that uses the ‘obj’ object we passed to it. In this case, let’s call the methods type() and color(), each of which is defined in the two classes ‘Tomato’ and ‘Apple’. Now, you have to create instantiations of both the ‘Tomato’ and ‘Apple’ classes if we don’t have them already:

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) 
func(obj_apple)

Output:

Vegetable
Red
Fruit
Red

Polymorphism with Class Methods

Python uses two different class types in the same way. Here, you have to create a for loop that iterates through a tuple of objects. Next, you have to call the methods without being concerned about which class type each object is. We assume that these methods actually exist in each class.

Here is an example to show polymorphism with class:

class India():
     def capital(self):
       print("New Delhi")

     def language(self):
       print("Hindi and English")

class USA():
     def capital(self):
       print("Washington, D.C.")

     def language(self):
       print("English")

obj_ind = India()
obj_usa = USA()
for country in (obj_ind, obj_usa):
country.capital()
country.language()

Output:

New Delhi
Hindi and English
Washington, D.C.
English

Polymorphism with Inheritance

Polymorphism in python defines 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. Also, it is possible to modify a method in a child class that it has inherited from the parent class.

This is mostly used in cases where the method inherited from the parent class doesn’t fit the child class. This process of re-implementing a method in the child class is known as Method Overriding. Here is an example that shows polymorphism with inheritance:

class Bird:
     def intro(self):
       print("There are different types of birds")

     def flight(self):
       print("Most of the birds can fly but some cannot")

class parrot(Bird):
     def flight(self):
       print("Parrots can fly")

class penguin(Bird):
     def flight(self):
       print("Penguins do not fly")

obj_bird = Bird()
obj_parr = parrot()
obj_peng = penguin()

obj_bird.intro()
obj_bird.flight()

obj_parr.intro()
obj_parr.flight()

obj_peng.intro()
obj_peng.flight()

Output:

There are different types of birds
Most of the birds can fly but some cannot
There are different types of bird
Parrots can fly
There are many types of birds
Penguins do not fly

These are different ways to define polymorphism in Python. With this, we have come to the end of our article. I hope you understood what is polymorphism and how it is used in Python.

Got a question for us? Please mention it in the comments section of “Polymorphism in Python” and we will get back to you.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 23rd March,2024

23rd March

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 20th April,2024

20th April

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is Polymorphism in OOPs programming?

edureka.co