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:

  1. returnType → type of value returned (use void if nothing)
  2. methodName → descriptive name of the method
  3. parameters → 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

  1. Methods can have parameters to pass data
  2. 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:

  1. Same method name with different parameters (type, number, or order)
  2. Provides flexibility and code readability

Rules for Method Overloading:

  1. Methods must have same name
  2. Methods must differ in parameters (number/type/order)
  3. 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:

  1. Java determines which method to call at compile-time based on parameters

5. Advantages of Method Overloading

  1. Improves code readability
  2. Reuses method name for similar functionality
  3. 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

  1. Method → performs a specific task, can take parameters and return values
  2. Method Overloading → same name, different parameters
  3. Overloading provides flexibility, readability, and compile-time polymorphism
  4. Methods promote modular and reusable code in Java