Java Methods and Method Overloading – Complete Guide with Examples
Learn how to define and use methods in Java, including method overloading concepts, syntax, and examples for efficient code reuse and modular programming.
Methods and Method Overloading in Java – Complete Detailed Tutorial
In Java, methods are blocks of code that perform a specific task.
Methods help in code reuse, modular programming, and maintainability.
1. Defining a Method in Java
Syntax:
returnType methodName(parameters) {
// method body
return value; // if returnType is not void
}
Key Points:
returnType→ type of value returned (usevoidif nothing)methodName→ descriptive name of the methodparameters→ optional inputs to the method
2. Example – Basic Method
class Calculator {
// method to add two numbers
int add(int a, int b) {
return a + b;
}
// method to display message
void greet() {
System.out.println("Welcome to Java Methods!");
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
int sum = calc.add(10, 20);
System.out.println("Sum: " + sum);
calc.greet();
}
}
Output:
Sum: 30
Welcome to Java Methods!
3. Method Parameters and Return Values
- Methods can have parameters to pass data
- Methods can return a value
Example – method with multiple parameters:
int multiply(int x, int y) {
return x * y;
}
4. Method Overloading in Java
Method Overloading:
- Same method name with different parameters (type, number, or order)
- Provides flexibility and code readability
Rules for Method Overloading:
- Methods must have same name
- Methods must differ in parameters (number/type/order)
- Return type alone cannot overload a method
Example – Method Overloading
class Calculator {
int add(int a, int b) { // 2 integers
return a + b;
}
double add(double a, double b) { // 2 doubles
return a + b;
}
int add(int a, int b, int c) { // 3 integers
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 add(int,int)
System.out.println(calc.add(5.5, 6.5)); // calls add(double,double)
System.out.println(calc.add(1, 2, 3)); // calls add(int,int,int)
}
}
Output:
30
12.0
6
Explanation:
- Java determines which method to call at compile-time based on parameters
5. Advantages of Method Overloading
- Improves code readability
- Reuses method name for similar functionality
- Compile-time polymorphism (static polymorphism)
6. Example Program – Methods and Overloading
class Employee {
String name;
double salary;
// display employee info
void display(String name) {
System.out.println("Employee Name: " + name);
}
void display(String name, double salary) {
System.out.println("Employee Name: " + name + ", Salary: $" + salary);
}
double bonus(double salary, double percentage) {
return salary * percentage / 100;
}
double bonus(double salary) { // overloaded method
return salary * 10 / 100; // default 10% bonus
}
}
public class Main {
public static void main(String[] args) {
Employee emp = new Employee();
emp.display("Chinmaya");
emp.display("Chinmaya", 50000);
System.out.println("Bonus: $" + emp.bonus(50000));
System.out.println("Bonus: $" + emp.bonus(50000, 20));
}
}
Output:
Employee Name: Chinmaya
Employee Name: Chinmaya, Salary: $50000.0
Bonus: $5000.0
Bonus: $10000.0
7. Summary
- Method → performs a specific task, can take parameters and return values
- Method Overloading → same name, different parameters
- Overloading provides flexibility, readability, and compile-time polymorphism
- Methods promote modular and reusable code in Java