Python_e-content_revised Polymorphism.pptx
Python_e-content_revised Polymorphism.pptx
POLYMORPHISM
1
Dr. BMN College of Home Science (Autonomous) || NAAC A+ Grade with 3.69/4 CGPA || Smt. K. G.
Shah Dept. of Computer Applications || 06th May 2020
POLYMORPHISM
● The word polymorphism means having many forms. In programming,
polymorphism means the same function name (but different signatures) being
used for different types. The key difference is the data types and number of
arguments used in function.
● Example
● print(len(“geeks”))
● print(len([10,20,30]))
2
Dr. BMN College of Home Science (Autonomous) || NAAC A+ Grade with 3.69/4 CGPA || Smt. K. G.
Shah Dept. of Computer Applications || 06th May 2020
POLYMORPHISM
3
Dr. BMN College of Home Science (Autonomous) || NAAC A+ Grade with 3.69/4 CGPA || Smt. K. G.
Shah Dept. of Computer Applications || 06th May 2020
POLYMORPHISM
● We can use the concept of polymorphism while creating class methods as
Python allows different classes to have methods with the same name.
4
Dr. BMN College of Home Science (Autonomous) || NAAC A+ Grade with 3.69/4 CGPA || Smt. K. G.
Shah Dept. of Computer Applications || 06th May 2020
POLYMORPHISM
● We can use the concept of polymorphism while creating class methods as
Python allows different classes to have methods with the same name.
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(f"I am a cat. My name is {self.name}. I am {self.age} years old.")
def make_sound(self):
print("Meow")
5
Dr. BMN College of Home Science (Autonomous) || NAAC A+ Grade with 3.69/4 CGPA || Smt. K. G.
Shah Dept. of Computer Applications || 06th May 2020
POLYMORPHISM
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(f"I am a dog. My name is {self.name}. I am {self.age} years old.")
def make_sound(self):
print(“Bark")
POLYMORPHISM
Here, we have created two classes Cat and Dog. They share a similar structure
and have the same method names info() and make_sound().
However, notice that we have not created a common superclass or linked the
classes together in any way. Even then, we can pack these two different objects
into a tuple and iterate through it using a common animal variable. It is possible
due to polymorphism.
7
Dr. BMN College of Home Science (Autonomous) || NAAC A+ Grade with 3.69/4 CGPA || Smt. K. G.
Shah Dept. of Computer Applications || 30th April 2020