What is Polymorphism in OOPs programming - NareshIT
Naresh i Technologies
Only Institute to offer the 'Most Comprehensive eLearning Platform to suit the self-learning needs of all CMS and LMS
Introduction:
As you know Polymorphism?is an approach in an?object-oriented programming?language using which an object is going to satisfy the different behavior at different instances of time. Here it performs different things as per the object's class. With?Polymorphism, actually, we are going to send a message to multiple class objects, and every object responds appropriately according to the properties of the class. In Python, if we need to implement the same concept then it is very easy. As you know that Python is a more powerful language, and it has many enriched library packages with itself so we can achieve this concept easily here.
What is Polymorphism:
1.?????Polymorphism?is an approach in an?object-oriented programming?language using which an object is going to satisfy the different behavior at different instances of time.
2.?????As I have already discussed above that here in this process, we are going to send a message to multiple class objects, and every object responds appropriately according to the properties of the class.
3.?????In other cases, sometimes it may also be get defined as an object-oriented programming concept which is mostly used to refer to the ability of a?variable,?function,?or?object?to take on?multiple?forms.?
4.?????It is basically used to get implements the concept of function overloading, function overriding, and virtual functions approach in a program.
5.?????It is the method in an oops language that usually does different things based on the class of the object which is used to call it for its invocation.
?Polymorphism in Python:
?1.?????As Python is also used to support the concept of OOPS environment so if we need then we can be able to implement the concept of polymorphism here.
2.?????To implement the concept of Polymorphism as I have already discussed above that it usually does different things based on the class of the object which is used to call it for its invocation.
3.?????As it can be get achieved in many ways such as by implementing the concept of function overloading, function overriding, and virtual functions approach in the program.?
4.?????As you know that when we are going to have the inheritance concept where the child class is used to inherit the property of the base class but sometimes all properties are not fully get acquired in the child class.
5.?????Hence to overcome the same we need to redeclare the functions again in the child class which is called the overriding approach.
6.?????In Python we can use a different function,?class methods,?or objects to define polymorphism.
Note:
It should also be noted that Python also has some inbuilt polymorphic functions. Let us consider the following example which will let you know about them.
Examples:
Python program to demonstrate in-built polymorphic functions
??
# len() being used for a string
print(len("Hello Python"))
??
# len() being used for a list
print(len([10, 20, 30]))
?Similarly, like inbuilt, user-defined polymorphic functions are also allowed in Python. If we need to create it then it can be get done in the following ways.
?# A simple Python function to demonstrate?Polymorphism
?def add(x, y, z = 0):?
????return x + y+z
??
# Driver code?
print(add(2, 3))
print(add(2, 3, 4))
?
When the program will run the output will be
5
9
Polymorphism with Function and Objects:
As I have discussed above if we need to achieve polymorphism using the function and object-based approach then we need to create a function that can take any?object, allowing for polymorphism.
Let us consider the following example where we are going to create the class and within that, we are going to create the respective instances using the function.
Example:
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)
When this program runs it will produce the output as
Vegetable
Red
Fruit
Red
Note:
In the above program, we have created 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.
Polymorphism with Class Methods:
Like similar to above if we need to achieve polymorphism using the class and object-based approach then we need to create a class first and then we have to define the function that can take any?object, allowing for polymorphism.
Let us consider the following example where we are going to create the class and within that, we are going to create the respective instances using the function.
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()
for country in (obj_ind):
????country.capital()
????country.language()
????country.type()
?
Output:
When the program is allowed to get run then the following output will be get observed.
New Delhi is the capital of India.
Hindi is the most widely spoken language in India.
India is a developing country.
?
Polymorphism with Inheritance:
1.?????To implement the concept of Polymorphism in Python using the Inheritance approach as I have already discussed above that it usually does different things based on the class of the object which is used to call it for its invocation.
2.?????As you know that when we are going to have the inheritance concept where the child class is used to inherit the property of the base class but sometimes all properties are not fully get acquired in the child class.
3.?????As it can be get achieved in many ways such as by implementing the concept of function overloading, function overriding, and virtual functions approach in the program.?
Let us consider the following example which will let you understand the concept of Polymorphism in Python using the inheritance approach. But it should be get noted that we need to use the approach of method overriding over here as through this approach only the child class method can override over the base class method.
Program:
class Bird:
??def intro(self):
????print("There are many types of birds.")
??????
??def flight(self):
????print("Most of the 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()
??
obj_spr.intro()
obj_spr.flight()
??
obj_ost.intro()
obj_ost.flight()
?
When the program runs then it will produce the output as
There are many types of birds.
Most birds can fly but some cannot.
There are many types of birds.
Sparrows can fly.
There are many types of birds.
Ostriches cannot fly.
?
Scope @ NareshIT:
1.?????At NareshIT’s Python application Development program you will be able to get extensive hands-on training in front-end, middleware, and back-end technology.
2.?????It skilled you along with phase-end and capstone projects based on real business scenarios.
3.?????Here you learn the concepts from leading industry experts with content structured to ensure industrial relevance.
4.?????An end-to-end application with exciting features
You can contact us anytime for your?Python training, and from any part of the world.?Naresh I Technologies?caters to one of the best Python training in India.
Follow us on Linkedin:?https://bit.ly/NITLinkedIN