Method Overloading
Method Overloading
Method overloading is a feature in Java that allows you to define multiple methods in the same class with the same name but different parameters.
The parameters can differ in terms of their number, type, or both. Java uses the method signature (method name and parameter list) to determine which overloaded method to call based on the arguments provided. Here's how method overloading works in Java:
Method overloading enhances the program's clarity and comprehensibility.
Different Ways of Method Overloading in Java
By Changing the Number of Parameters.
By Changing Data Types of the Arguments.
By Changing the Order of the Parameters of Methods.
Method overloading in Java allows you to define multiple methods with the same name but a different number of parameters. This enables you to create methods that perform similar operations but can accept varying amounts of data. Here's an example of method overloading by changing the number of arguments:
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
}
In this example, there are two add methods. One takes two integers as parameters, and the other takes three integers.
This allows you to perform addition with either two or three integers using the same method name.
Usage example:
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
int sum2 = calculator.add(5, 10);
int sum3 = calculator.add(2, 3, 5);
System.out.println("Sum of 2 numbers: " + sum2);
System.out.println("Sum of 3 numbers: " + sum3);
}
}
Output:-
Sum of 2 numbers: 15
Sum of 3 numbers: 10
In this example, the first add method is invoked with two integers, and the second add method is invoked with three integers. The appropriate method is selected based on the number of arguments provided.
2.By Changing Data Types of the Arguments.
Method overloading in Java allows you to define multiple methods with the same name but different parameter types. This enables you to perform similar operations on different types of data. Here's an example of method overloading by changing the data type of arguments:
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
In this example, there are two add methods. One takes two integers as parameters, and the other takes two doubles. This allows you to perform addition on both integers and floating-point numbers using the same method name.
Usage example:
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
int intSum = calculator.add(5, 10);
double doubleSum = calculator.add(2.5, 3.5);
System.out.println("Integer Sum: " + intSum);
System.out.println("Double Sum: " + doubleSum);
}
}
Output:-
Integer Sum: 15
Double Sum: 6.0
3.By Changing the Order of the Parameters of Methods.
Method overloading in Java also allows you to change the order of the parameters of methods while keeping the same method name. Java differentiates between overloaded methods based on the sequence and data types of the provided arguments.
Here's an example of method overloading by changing the order of method parameters:
class Student {
// Method 1
public void StudentId(String name, int roll_no)
{
System.out.println("Name :" + name + " " + "Roll-No :" + roll_no);
}
// Method 2
public void StudentId(int roll_no, String name)
{
// Again printing name and id of person
System.out.println("Roll-No :" + roll_no + " " + "Name :" + name);
}
}
// Class 2
// Main class
class Demo {
// Main function
public static void main(String[] args)
{
// Creating object of above class
Student obj = new Student();
// Passing name and id
// Note: Reversing order
obj.StudentId("Spyder", 1);
obj.StudentId(2, "Kamlesh");
}
}
Output:-
Name :Spyder Roll-No :1
Roll-No :2 Name :Kamlesh
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(100, 200));
System.out.println(s.sum(100, 200, 300));
System.out.println(s.sum(100.5, 200.5));
}
}
Output:-
300
600
301.0