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:
- What is Polymorphism?
- Polymorphism in Python
- Polymorphism with Function and Objects
- Polymorphism with Class Methods
- Polymorphism with Inheritance
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.
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)
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()
New Delhi Hindi and English Washington, D.C. English
Upskill for Higher Salary with Python Programming Courses
Course Name | Upcoming Batches | Fees |
Python Certification Course | 20th April 2024 (Weekend Batch) | ₹17,795 |
Python Certification Course | 18th May 2024 (Weekend Batch) | ₹15,125 |
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.