Overloading refers to making a method perform different operations based on the nature of its arguments.
Unlike in other programming languages, methods cannot be explicitly overloaded in Python but can be implicitly overloaded.
In order to include optional arguments, we assign default values to those arguments rather than creating a duplicate method with the same name. If the user chooses not to assign a value to the optional parameter, a default value will automatically be assigned to the variable.
xxxxxxxxxx
class Employee:
# defining the properties and assigning them None to the
def __init__(self, ID=None, salary=None, department=None):
self.ID = ID
self.salary = salary
self.department = department
# method overloading
def demo(self, a, b, c, d=5, e=None):
print("a =", a)
print("b =", b)
print("c =", c)
print("d =", d)
print("e =", e)
def tax(self, title=None):
return (self.salary * 0.2)
def salaryPerDay(self):
return (self.salary / 30)
# cerating an object of the Employee class
Steve = Employee()
# Printing properties of Steve
print("Demo 1")
Steve.demo(1, 2, 3)
print("\n")
print("Demo 2")
Steve.demo(1, 2, 3, 4)
print("\n")
print("Demo 3")
Steve.demo(1, 2, 3, 4, 5)
xxxxxxxxxx
Overloading mean same method name and different parameter,
it can happen in same class. it's a feature that
allows us to have more than one method with same name.
Example: sort method of Arrays class
Arrays.sort(int[] arr)
Arrays.sort(String[] arr)
.
Method overloading improves the reusability and readability.
and it's easy to remember
(one method name instead of remembering multiple method names)
xxxxxxxxxx
- Same method name but different parameters
- According to different parameters, different method is loaded
xxxxxxxxxx
// Java program to demonstrate working of method
// overloading in Java.
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y)
{
return (x + y);
}
// Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum(). This sum takes two double parameters
public double sum(double x, double y)
{
return (x + y);
}
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
xxxxxxxxxx
Method overloading is providing two separate methods in a class
with the same name but different arguments, while the method return type
may or may not be different, which allows us to reuse the same method name.
xxxxxxxxxx
public class methodOverloading{
public static void main(String[] args) {
int intNum_01 = 43;
int intNum_02 = 55;
double doubleNum_01 = 4.3;
double doubleNum_02 = 3.23;
int result_01 = minimumNumFun(intNum_01 , intNum_02 );
// same function name with different parameters
double result_02 = minimumNumFun(doubleNum_01 , doubleNum_01 );
System.out.println("Minimum Value = " + result_01);
System.out.println("Minimum Value = " + result_02);
}
// for integer
public static int minimumNumFun(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
// for double
public static double minimumNumFun(double n1, double n2) {
double min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
xxxxxxxxxx
import java.io.*;
class MethodOverloadingEx {
static int add(int a, int b)
{
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
public static void main(String args[])
{
System.out.println("add() with 2 parameters");
System.out.println(add(4, 6));
System.out.println("add() with 3 parameters");
System.out.println(add(4, 6, 7));
}
}
Method Overloading is also known as Static Polymorphism.
xxxxxxxxxx
class OverloadingCalculation3{
void sum(int a,long b){System.out.println("a method invoked");}
void sum(long a,int b){System.out.println("b method invoked");}
public static void main(String args[]){
OverloadingCalculation3 obj=new OverloadingCalculation3();
obj.sum(20,20);//now ambiguity
}
}
xxxxxxxxxx
public class MathOperations
{
public int Multiply(int a, int b) => a * b;
public double Multiply(double a, double b) => a * b;
public decimal Multiply(decimal a, decimal b) => a * b;
}