- Learning Objectives
- Prerequisites
- Introduction
- What is a Method?
- What is Method Overloading?
- Does Python Support Method Overloading?
- Why Python Does Not Support Method Overloading
- How Python Achieves Method Overloading
- Using Default Arguments
- Using *args
- Using **kwargs
- Method Overloading vs Method Overriding
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Understand Method Overloading.
- Explain why Python does not support true Method Overloading.
- Simulate Method Overloading using Python techniques.
- Differentiate Method Overloading and Method Overriding.
- Implement flexible methods using default arguments, *args, and **kwargs.
Before learning this chapter, you should know:
- Classes
- Objects
- Methods
- Constructors
- Constructor Overloading
- Polymorphism
Methods define the behavior of objects.
Sometimes,
we want the same method to perform different tasks depending on the number or type of arguments supplied.
For example,
a Calculator may perform addition using
add()
add(a)
add(a, b)
add(a, b, c)
In Java,
this is called Method Overloading.
Python follows a different approach.
Instead of supporting multiple methods with the same name,
Python provides flexible programming features such as
- Default Arguments
- Variable-Length Arguments (
*args) - Keyword Arguments (
**kwargs)
These features make Method Overloading unnecessary.
A Method is a function defined inside a class that performs a specific operation on an object.
Example
class Student:
def display(self):
print("Student Details")Methods define the behavior of objects.
Method Overloading is the process of defining multiple methods with the same name but different parameter lists within the same class.
The appropriate method is selected based on the arguments supplied during the method call.
class Calculator {
void add() {}
void add(int a) {}
void add(int a, int b) {}
void add(int a, int b, int c) {}
}The compiler automatically selects the appropriate method.
No.
Python does not support true Method Overloading.
A class can have only one method with a particular name.
If multiple methods with the same name are declared,
the last definition replaces all previous definitions.
class Demo:
def show(self):
print("First Method")
def show(self, name):
print("Second Method")Only the second method exists.
The first method is overwritten.
Python is a dynamically typed language.
It does not differentiate methods based on
- Number of Parameters
- Type of Parameters
Instead,
Python encourages writing flexible methods using
- Default Arguments
*args**kwargs
This makes separate overloaded methods unnecessary.
Although Python does not support true Method Overloading,
similar behavior is achieved using:
- Default Arguments
- Variable-Length Arguments (
*args) - Keyword Arguments (
**kwargs)
Default arguments make parameters optional.
Example
class Calculator:
def add(self, a, b=0):
return a + bMethod Calls
calculator.add(10)
calculator.add(10, 20)*args accepts any number of positional arguments.
Example
class Calculator:
def add(self, *numbers):
print(numbers)Example Calls
calculator.add()
calculator.add(10)
calculator.add(10, 20)
calculator.add(10, 20, 30)**kwargs accepts any number of keyword arguments.
Example
class Student:
def details(self, **kwargs):
print(kwargs)Example Calls
student.details(name="Rahul")
student.details(name="Rahul", age=21)
student.details(name="Rahul", age=21, course="Python")| Method Overloading | Method Overriding |
|---|---|
| Same Class | Parent and Child Classes |
| Same Method Name | Same Method Name |
| Different Parameter Lists | Same Parameter List |
| Compile-Time Concept | Run-Time Concept |
| Not directly supported in Python | Fully supported in Python |
Method Called
↓
Python Searches Method Name
↓
Last Method Definition Found
↓
Method Executes
If duplicate method names exist,
Python keeps only the last definition.
- Flexible programming.
- Less code duplication.
- Easy maintenance.
- Cleaner class design.
- Supports dynamic argument handling.
Flexible methods are commonly used in:
- Calculator Applications
- Banking Systems
- Employee Management Systems
- Inventory Systems
- Data Processing Libraries
- Web Frameworks
In this part, we learned:
- Methods
- Method Overloading
- Why Python does not support true Method Overloading
- Default Arguments
*args**kwargs- Method Overloading vs Method Overriding
- Internal Working
Write a Python program to demonstrate that Python does not support true Method Overloading.
class Calculator:
def add(self):
print("First Method")
def add(self, a):
print("Second Method")
print("Value :", a)
calculator = Calculator()
calculator.add(10)Second Method
Value : 10
Although two methods with the same name are declared,
Python keeps only the last method definition.
The first method is overwritten.
Write a Python program to simulate Method Overloading using default arguments.
class Calculator:
def add(self, a, b=0):
return a + b
calculator = Calculator()
print(calculator.add(10))
print(calculator.add(10, 20))10
30
When only one argument is supplied,
the default value of
b = 0is used.
Write a Python program using *args.
class Calculator:
def add(self, *numbers):
print(sum(numbers))
calculator = Calculator()
calculator.add()
calculator.add(10)
calculator.add(10, 20)
calculator.add(10, 20, 30)0
10
30
60
*numbers stores all positional arguments inside a tuple.
The built-in
sum()function calculates the total.
class Student:
def details(self, **kwargs):
for key, value in kwargs.items():
print(key, ":", value)
student = Student()
student.details(name="Rahul")
student.details(name="Rahul", age=21)
student.details(name="Rahul", age=21, course="Python")name : Rahul
name : Rahul
age : 21
name : Rahul
age : 21
course : Python
**kwargs stores keyword arguments inside a dictionary.
class Calculator:
def add(self, a=0, *numbers):
total = a + sum(numbers)
print(total)
calculator = Calculator()
calculator.add()
calculator.add(10)
calculator.add(10, 20)
calculator.add(10, 20, 30)0
10
30
60
The first parameter has a default value.
Remaining values are stored inside numbers.
class Student:
def details(self, name, age=18, course="Python"):
print("Name :", name)
print("Age :", age)
print("Course :", course)
student = Student()
student.details("Rahul")
student.details("Priya", 21)
student.details("Akhil", 22, "Java")Name : Rahul
Age : 18
Course : Python
Name : Priya
Age : 21
Course : Python
Name : Akhil
Age : 22
Course : Java
Default arguments provide flexibility without creating multiple methods.
class Employee:
def display(self, **details):
for key, value in details.items():
print(key, ":", value)
employee = Employee()
employee.display(name="Rahul", salary=50000, department="HR")name : Rahul
salary : 50000
department : HR
Keyword arguments are useful when different objects contain different information.
class Calculator:
def multiply(self, *numbers):
result = 1
for number in numbers:
result *= number
print(result)
calculator = Calculator()
calculator.multiply(2, 3)
calculator.multiply(2, 3, 4)6
24
*args allows the method to work with any number of arguments.
class Demo:
def show(self, *args, **kwargs):
print("Positional :", args)
print("Keyword :", kwargs)
demo = Demo()
demo.show(10, 20, name="Rahul", age=21)Positional : (10, 20)
Keyword : {'name': 'Rahul', 'age': 21}
Methods can accept both positional and keyword arguments together.
class Bank:
def deposit(self, amount=0):
print("Deposited :", amount)
bank = Bank()
bank.deposit()
bank.deposit(5000)Deposited : 0
Deposited : 5000
Default arguments make methods more flexible and eliminate the need for multiple overloaded methods.
Method Called
↓
Arguments Passed
↓
Method Parameters Matched
↓
Method Executes
↓
Output Produced
Calculator Object
│
▼
+---------------------------+
add()
multiply()
+---------------------------+
- Flexible methods.
- Cleaner code.
- Less duplication.
- Easy maintenance.
- Supports optional arguments.
- Improves readability.
- Use default arguments when only a few parameters are optional.
- Use
*argsfor variable-length positional arguments. - Use
**kwargsfor optional keyword arguments. - Keep methods focused on a single responsibility.
- Validate input before processing.
Writing multiple methods with the same name.
❌ Incorrect
def show():
pass
def show(name):
passOnly the last method remains.
Using *args when fixed parameters are sufficient.
Choose the simplest approach that meets the requirement.
Using **kwargs without checking required keys.
Always validate expected keys before using them.
Confusing Method Overloading with Method Overriding.
Method Overloading
- Same class
- Different parameter lists
Method Overriding
- Parent and Child classes
- Same method signature
Method Overloading is the process of creating multiple methods with the same name but different parameter lists.
No.
Python allows only one method with a given name inside a class.
Python is dynamically typed and instead provides default arguments, *args, and **kwargs for flexible method definitions.
Using:
- Default Arguments
*args**kwargs
A tuple containing positional arguments.
A dictionary containing keyword arguments.
The last method definition replaces all previous ones.
Method Overloading occurs within the same class using different parameter lists.
Method Overriding occurs between parent and child classes using the same method signature.
When only a few parameters are optional and predictable.
Calculator methods, Student information methods, Banking operations, Product configuration, and Employee management methods that accept optional parameters.
- Create a Calculator using default arguments.
- Create a Student class using
*args. - Create an Employee class using
**kwargs. - Create a Product class using both
*argsand**kwargs. - Demonstrate why multiple methods with the same name do not work in Python.
- Python supports only one method with a given name.
- Default arguments simulate optional parameters.
*argsstores positional arguments in a tuple.**kwargsstores keyword arguments in a dictionary.- Method Overloading is simulated, not directly supported.
In this chapter, we learned:
- Method Overloading
- Why Python does not support true Method Overloading
- Default Arguments
*args**kwargs- Practical Programs
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision