01 / 21

Method
Overloading

Learn how Java lets you define multiple methods with the same name but different parameters: one of the most powerful tools for writing clean, flexible code.

Java

What is Method Overloading?

In Java, method overloading means defining two or more methods in the same class that share the same name but have different parameter lists. The compiler decides which version to call based on the arguments you pass.

Why Overloading Matters

Readability
One intuitive name instead of addInt, addDouble, addThree variations.
Flexibility
Callers pass different types or counts of arguments without learning new names.
Polymorphism
Overloading is compile-time polymorphism: the method chosen is resolved before the program runs.

One Name, Many Signatures

add( ) add(int a, int b) add(double a, double b) add(int a, int b, int c) add(String a, String b) Compiler picks the right version based on your arguments add(3, 5) → calls add(int, int)

The Rules of Overloading

1
Same method name within the same class.
2
Different parameter list: vary the number, type, or order of parameters.
3
Return type alone is not enough to overload: two methods differing only in return type will cause a compile error.
4
Access modifiers and exceptions can differ, but they do not contribute to the overload signature.

What Is a Method Signature?

public int add(int a, double b) Signature: name + parameter types Not part of signature Included in Signature Method name + param types/order Not in Signature Return type, modifiers, param names

Overloading vs Overriding

Overloading Overriding
Where Same class Parent → Child class
Parameters Must differ Must be identical
Return type Can differ Same or covariant
Polymorphism Compile-time Run-time
Annotation None required @Override

How to Apply Overloading

1
Identify the action: pick a clear, descriptive method name (e.g., calculate, print, findMax).
2
List the variations: decide how many different parameter combinations callers might need.
3
Write each version: keep the same name but change the number, type, or order of parameters.
4
Verify uniqueness: ensure no two versions share the exact same parameter type list.
5
Test each call: compile and call each overloaded version to confirm the compiler resolves correctly.

Example 1: Different Number of Parameters

class Calculator {
  static int add(int a, int b) {
    return a + b;
  }

  static int add(int a, int b, int c) {
    return a + b + c;
  }
}

Example 2: Different Types

class Printer {
  static void display(int value) {
    System.out.println("Integer: " + value);
  }

  static void display(String value) {
    System.out.println("String: " + value);
  }

  static void display(double value) {
    System.out.println("Double: " + value);
  }
}

Example 3: Different Order of Types

class OrderDemo {
  static void show(int a, String b) {
    System.out.println(a + " then " + b);
  }

  static void show(String a, int b) {
    System.out.println(a + " then " + b);
  }
}

Type Promotion and Overloading

If no exact match exists, Java automatically promotes smaller types to larger ones: byte → short → int → long → float → double.

class PromoDemo {
  static void test(double d) {
    System.out.println("double: " + d);
  }
}

// In main:
PromoDemo.test(5); // int 5 promoted to double 5.0

What Does Not Count?

// COMPILE ERROR: same parameter types!
static int add(int a, int b) { return a + b; }
static double add(int x, int y) { return x + y; }
✕ Return type only
Changing only the return type is not valid overloading.
✕ Parameter names only
Renaming a to x does not change the signature.

Real-World Use Case

class Logger {
  static void log(String message) {
    System.out.println("[INFO] " + message);
  }

  static void log(String message, int code) {
    System.out.println("[ERR " + code + "] " + message);
  }

  static void log(String message, String level) {
    System.out.println("[" + level + "] " + message);
  }
}

Common Mistakes

✕ Return Type Confusion
Thinking that changing only the return type (e.g., int vs double) makes a valid overload. It does not.
✕ Mixing Up Override
Confusing overloading (same class) with overriding (subclass). They are different mechanisms.
✕ Ambiguous Calls
Creating overloads like add(int, double) and add(double, int) then calling add(5, 5): the compiler cannot decide.

Summary Cheat Sheet

Criterion Valid Overload? Example
Different number of params ✓ Yes add(int) vs add(int, int)
Different param types ✓ Yes add(int) vs add(double)
Different param order ✓ Yes f(int, String) vs f(String, int)
Different return type only ✕ No int add(int) vs double add(int)
Different param names only ✕ No add(int a) vs add(int x)

Question 1 of 5

Which of the following is required for two methods to be considered overloaded?

Question 2 of 5

Given these two methods in the same class:
int calc(int a) and double calc(int a)
What happens when you compile?

Question 3 of 5

A class has display(int x) and display(double x). What happens when you call display(5)?

Question 4 of 5

Which pair of methods is a valid example of overloading?

Question 5 of 5

Method overloading is an example of which type of polymorphism?