0% found this document useful (0 votes)
1 views

Python_e-content_revised Polymorphism.pptx

The document discusses the concept of polymorphism in programming, particularly in Python, where the same function name can be used with different data types and argument numbers. It provides examples through class definitions for 'Cat' and 'Dog', demonstrating how methods can be called interchangeably despite the classes not having a common superclass. The document emphasizes the flexibility of polymorphism in object-oriented programming.

Uploaded by

ahire6687
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Python_e-content_revised Polymorphism.pptx

The document discusses the concept of polymorphism in programming, particularly in Python, where the same function name can be used with different data types and argument numbers. It provides examples through class definitions for 'Cat' and 'Dog', demonstrating how methods can be called interchangeably despite the classes not having a common superclass. The document emphasizes the flexibility of polymorphism in object-oriented programming.

Uploaded by

ahire6687
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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

E-content Developed By: Shilpa S Surulkar, Assistant


Professor, Smt. K. G. Shah Dept. of Computer
Applications, Dr. BMN College of Home Science

Special Outreach sessions in view of closure of college on


account of COVID-19 Advisory

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.

● We can then later generalize calling these methods by disregarding the


object we are working with.

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")

cat1 = Cat("Kitty", 2.5)


dog1 = Dog("Fluffy", 4)
for animal in (cat1, dog1):
animal.make_sound()
animal.info()
animal.make_sound()
6
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

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

Stay Home, Stay Safe #fightagainstcorona

You might also like