2. Polymorphism is a fundamental concept in object-
oriented programming (OOP) that allows objects of
different types to be treated as objects of a common
type. Python supports polymorphism through both
duck typing and method overloading.
3. DUCK TYPING
In Python, polymorphism is often achieved through
duck typing. This means that the type or the class
of an object is less important than the methods and
properties it possesses. If an object supports a
particular method or property, it can be used
interchangeably with other objects that support the
same method or property.
4. DUCK TYPING - EXAMPLE
class Dog:
def speak(self):
return "Woof!"
class Cat:
def speak(self):
return "Meow!"
def animal_speak(animal):
return animal.speak()
dog = Dog()
cat = Cat()
print(animal_speak(dog)) # Output: Woof!
print(animal_speak(cat)) # Output: Meow!
5. METHOD OVERLOADING
Python does not support traditional method
overloading (having multiple methods with the
same name but different parameters) like some
other languages do. However, you can achieve a
form of method overloading using default parameter
values and variable numbers of arguments.
6. METHOD OVERLOADING - EXAMPLE
class MathOperations:
def add(self, x, y=None, z=None):
if y is not None and z is not None:
return x + y + z
elif y is not None:
return x + y
else:
return x
math_ops = MathOperations()
print(math_ops.add(2)) # Output: 2
print(math_ops.add(2, 3)) # Output: 5
print(math_ops.add(2, 3, 4)) # Output: 9
7. ADVANTAGES
Polymorphism allows for the creation of more generic and
reusable code. A single interface can be used to represent
different types of objects, promoting code reusability.
Polymorphism makes it easier to extend and modify code.
New classes can be added without altering existing code, as
long as they adhere to the common interface.
Polymorphic code tends to be more readable and
maintainable. Code that operates on a higher level of
abstraction (e.g., using interfaces or abstract classes) is often
easier to understand and modify.
Polymorphism allows for dynamic binding, where the specific
method implementation is determined at runtime. This
enables more flexibility in program execution and facilitates
better adaptation to changes.
8. DISADVANTAGES
Overuse or misuse of polymorphism can lead to complex and
hard-to-understand code. It's important to strike a balance
and use polymorphism judiciously where it adds value to the
design.
Dynamic dispatch, a mechanism that enables polymorphism,
may introduce a slight performance overhead compared to
static binding. However, in many cases, the performance
impact is negligible.
Since method resolution is determined at runtime, errors
related to missing or incorrect methods may not be caught
until the program is executed. This can make debugging more
challenging.
Understanding and implementing polymorphism might have a
steeper learning curve for programmers new to object-
oriented concepts. It requires a solid understanding of
inheritance, interfaces, and dynamic dispatch.