Method overloading in Java allows you to define multiple methods with the same name but with different method signatures. A method signature is determined by:
- The number of parameters
- The type of parameters
- The order of parameters
Important Points of Method Overloading
1. Same Method Name: All overloaded methods have the same name.
2. Different Parameter List: The methods differ in the number, type, or order of parameters.
3. Compile-Time Resolution: Overloaded methods are determined during compile-time, based on the method signature (parameters).
4. Return Type: The return type can differ, but it cannot be used to distinguish overloaded methods. That is, you can’t overload a method just by changing the return type.
Why Use Method Overloading?
Code Readability: Allows you to use the same method name for different operations, improving code clarity.
Code Reusability: Reduces the need to create different method names for similar operations.
Flexibility: Makes the method more flexible, allowing it to accept different types of data.
Example 1: Overloading by Changing the Number of Parameters
You can overload a method by changing the number of parameters passed.
class Calculator {
// Method to add two integers
int add(int a, int b) {
return a + b;
}
// Method to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(10, 20)); // Calls the method with two integers
System.out.println(calc.add(10, 20, 35)); // Calls the method with three integers
}
}
Output:
65
Explanation: The method add() is overloaded to accept either two or three int parameters.
Example 2: Overloading by Changing the Type of Parameters
You can also overload a method by changing the types of parameters.
class Calculator {
// Method to add two integers
int add(int a, int b) {
return a + b;
}
// Method to add two double values
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(10, 20)); // Calls the method with two integers
System.out.println(calc.add(10.5, 20.4)); // Calls the method with two doubles
}
}
Output:
30.9
Explanation: The method add() is overloaded to accept either integers or doubles.
Example 3: Overloading by Changing the Order of Parameters
Method overloading can also be achieved by changing the order of parameters.
class Printer {
// Method to print an integer and a string
void print(int a, String b) {
System.out.println(a + " " + b);
}
// Overloaded method to print a string and an integer
void print(String b, int a) {
System.out.println(b + " " + a);
}
}
public class Main {
public static void main(String[] args) {
Printer printer = new Printer();
printer.print(9, "October");
printer.print("October", 9);
}
}
Output:
October 9
Explanation: The method print() is overloaded to handle the same data types but in a different order.
Overloading Constructors
Constructor overloading occurs when a class has multiple constructors with different parameter lists.
Example of Constructor Overloading
class Employee {
String name;
String designation;
int age;
// Constructor with one parameter
Employee(String name) {
this.name = name;
}
// Constructor with two parameters
Employee(String name, String designation) {
this.name = name;
this.designation = designation;
}
// Constructor with three parameters
Employee(String name, String designation, int age) {
this.name = name;
this.designation = designation;
this.age = age;
}
void display() {
System.out.println("Name: " + name + ", Designation: " + designation + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Employee emp1 = new Employee("John");
Employee emp2 = new Employee("John", "Manager");
Employee emp3 = new Employee("John", "Manager", 35);
emp1.display();
emp2.display();
emp3.display();
}
}
Output:
Name: John, Designation: Manager, Age: 0
Name: John, Designation: Manager, Age: 35
Note: Different constructors allow creating a Employee object with varying amounts of information.