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

UNIT 5 - Polymorphism

Polymorphism in python

Uploaded by

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

UNIT 5 - Polymorphism

Polymorphism in python

Uploaded by

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

POLYMORPHISM

Polymorphism is one of the core concepts of Object-Oriented Programming (OOP). It allows


objects of different classes to be treated as objects of a common superclass.

Types of Polymorphism

1. Compile-time Polymorphism (Static Polymorphism)


o Achieved using method overloading, which Python doesn't support directly.
However, we can simulate it using default arguments or variable-length
arguments.
2. Run-time Polymorphism (Dynamic Polymorphism)
o Achieved through method overriding, where a method in a derived class has the
same name as a method in its base class.

Method Overriding:

 Child classes inherit methods from their parent class.


 The child class can redefine these methods to provide specific implementations.
 This allows for different behaviors based on the object's type.

class Animal:
def make_sound(self):
print("Some generic animal sound")

class Dog(Animal):
def make_sound(self):
print("Woof!")

class Cat(Animal):
def make_sound(self):
print("Meow!")

def animal_sound(animal):
animal.make_sound()

# Creating objects of Dog and Cat


dog = Dog()
cat = Cat()

# Both objects are treated as Animal types


animal_sound(dog) # Output: Woof!
animal_sound(cat) # Output: Meow!
In this example, the animal_sound function is polymorphic because it can accept any object that
is a subclass of Animal and call the make_sound method on it, regardless of the specific type of
the object.

Method Overloading (Simulated with Default Arguments)

Python doesn’t support true method overloading, but we can simulate it by using default
arguments.

class Calculator:
def add(self, a, b=0, c=0):
return a + b + c

# Creating object
calc = Calculator()

# Call with 2 arguments


print(calc.add(5, 3)) # Output: 8

# Call with 3 arguments


print(calc.add(5, 3, 2)) # Output: 10

# Call with 1 argument


print(calc.add(5)) # Output: 5

Output:

Hello, World!

Hello, Python!

Operator Overloading
Python also supports operator overloading, which allows us to redefine the behavior of operators
such as +, -, *, etc. when working with objects of a custom class.

we can define special methods like __add__, __sub__, __mul__, etc., to control how these
operators (+, -, *) behave with our objects.

Example: Overloading the + Operator

Overload the + operator to add two Point objects together, meaning that adding two points will
add their respective x and y coordinates.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y

# Overloading the '+' operator using the __add__ method


def __add__(self, other):
# Add the x and y coordinates of two points
return Point(self.x + other.x, self.y + other.y)

# Print the Point object in a readable format.

def __str__(self):
return f"Point({self.x}, {self.y})"

# Create two Point objects


p1 = Point(1, 2)
p2 = Point(3, 4)

# Add the two points using the overloaded '+' operator


p3 = p1 + p2 # This calls p1.__add__(p2)

# Print the result


print("The two points are added by using the overloaded '+' operator")
print(p3) # Output: Point(4, 6)

Output:

The two points are added by using the overloaded '+' operator

Point(4, 6)

Note:

__str__ method is a built-in magic method in Python that is used to define a human-readable
string representation of an object.

The __str__ method is not strictly required for operator overloading, but it is often used to make
objects easier to print and inspect in a readable way.

Benefits of Polymorphism

 Code Reusability: You can use a single function to process objects of different classes.
 Flexibility: New classes can be added with minimal changes to the code.
 Maintenance: Easier to maintain and extend.

Polymorphism is a powerful feature that helps in writing cleaner and more efficient code.

You might also like