0% found this document useful (0 votes)
6 views3 pages

Polymorphism in Python

Uploaded by

pugazhm125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Polymorphism in Python

Uploaded by

pugazhm125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Polymorphism in Python

Definition:
 It refers to the use of a single type entity (method, operator or object) to
represent different types in different scenarios.
 It enables a single interface to represent different underlying forms (data
types).
Types:
1. Method Overloading
2. Method Overriding
Method overloading

Definition:
In Python, method overloading can be simulated using default arguments or variable-
length arguments:

Example:
class Math:
def add(self, a, b, c=0):
return a + b + c
math = Math()
print(math.add(1, 2)) # Output: 3
print(math.add(1, 2, 3)) # Output: 6
Method Overriding
Definition:
 In method overriding, the child class provides a specific implementation of a
method that is already defined in its parent class
Example:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
dog = Dog() # Creating objects
dog.speak() # Output: Dog barks

You might also like