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.
Java Method Overloading – Interview Questions
Q 1: What is method overloading?
Ans: Multiple methods with the same name but different parameters.
Q 2: Is return type enough for overloading?
Ans: No, parameters must differ.
Q 3: When is overloading resolved?
Ans: At compile time.
Q 4: Can overloading occur in the same class?
Ans: Yes.
Q 5: Why use method overloading?
Ans: To improve code readability.
Java Method Overloading – Objective Questions (MCQs)
Q1. Which of the following statements is true about method overloading in Java?
Q2. Which of the following is a valid example of method overloading?
Q3. Can constructors in Java be overloaded?
Q4. Which of the following will NOT result in method overloading?
Q5. What happens if two overloaded methods have the same parameter types and number, but different return types?