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

(PWP) Presentation

The document explains the concepts of overloading and overriding in Python, both of which are related to polymorphism. Overloading allows multiple methods with the same name but different signatures in the same class, simulated in Python using default arguments and variable-length arguments, while overriding allows a subclass to provide a specific implementation for a method already defined in its superclass. Key differences include that overloading involves different signatures and is simulated, whereas overriding involves the same method signature and customizes inherited behavior.

Uploaded by

raivat15102004
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)
13 views3 pages

(PWP) Presentation

The document explains the concepts of overloading and overriding in Python, both of which are related to polymorphism. Overloading allows multiple methods with the same name but different signatures in the same class, simulated in Python using default arguments and variable-length arguments, while overriding allows a subclass to provide a specific implementation for a method already defined in its superclass. Key differences include that overloading involves different signatures and is simulated, whereas overriding involves the same method signature and customizes inherited behavior.

Uploaded by

raivat15102004
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

Programming with python (PWP) Presentation

Topic: Overloading and Overriding


What is Overloading and Overriding

Both overloading and overriding are concepts related to polymorphism, which allows objects of
different classes to respond to the same method call in different ways. However, they differ in their
implementation and purpose.

1. Overloading
● Definition: Overloading refers to the ability to define multiple methods or operators with the
same name but different signatures (number or types of arguments) within the same class.

● Python's Approach: Python doesn't support traditional method overloading in the way
languages like Java or C++ do. In those languages, you can define multiple methods with the
same name but different parameter lists.

● How Python Simulates Overloading: Python achieves a similar effect using:

○ Default arguments: You can define a single method with optional arguments, allowing
it to be called with varying numbers of arguments.
○ Variable-length arguments (*args and **kwargs): These allow a function to accept any
number of positional or keyword arguments.
○ Operator overloading: Python allows you to redefine the behavior of built-in operators
(like +, -, *, etc.) for custom classes.

Examples of Python's Overloading Simulations:

● Using Default Arguments:


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

calc = Calculator()
print(calc.add(5)) # Output: 5
print(calc.add(5, 10)) # Output: 15
print(calc.add(5, 10, 15)) # Output: 30

● Using Variable-Length Arguments (*args):


class Sum:
def calculate_sum(self, *numbers):
total = 0
for num in numbers:
total += num
return total
s = Sum()
print(s.calculate_sum(1, 2, 3)) # Output: 6
print(s.calculate_sum(1, 2, 3, 4, 5)) # Output: 15

● Operator Overloading:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y

def __add__(self, other):


return Point(self.x + other.x, self.y + other.y)

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

p1 = Point(1, 2)
p2 = Point(3, 4)
p3 = p1 + p2
print(p3) # Output: (4, 6)

2. Overriding
● Definition: Overriding occurs when a subclass provides a specific implementation for a
method that is already defined in its superclass.

● Purpose: It allows a subclass to customize or extend the behavior inherited from its parent
class.
● How it Works: When a method is called on an object of the subclass, Python first looks for the
method in the subclass. If found, it executes the subclass's implementation. Otherwise, it
searches in the superclass.

Example of Overriding:

class Animal:
def speak(self):
print("Animal speaks")

class Dog(Animal):
def speak(self):
print("Dog barks")

class Cat(Animal):
def speak(self):
print("Cat meows")

animal = Animal()
dog = Dog()
cat = Cat()

animal.speak() # Output: Animal speaks


dog.speak() # Output: Dog barks
cat.speak() # Output: Cat meows

Key Differences Summarized:

● Overloading:
○ Multiple methods with the same name in the same class.
○ Different signatures (number/types of arguments).
○ Simulated in Python using default arguments, *args, **kwargs, and operator
overloading.
● Overriding:
○ A subclass provides a different implementation for a method already defined in its
superclass.
○ Same method signature.
○ Used to customize or extend inherited behavior.

You might also like