Java Method Overloading

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 21 / 50 · English

Java Method Overloading

One useful name. Different parameter lists.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

  • Explain the key idea behind Method Overloading.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

Understand the idea

Overloaded methods share a name but differ in parameter types, number, or order. The compiler chooses an overload using the call and the declared types. Return type alone is not enough to distinguish overloads.

02 / Step by step

Explore the details

Method Overloading

With method overloading, multiple methods can have the same name with different parameters:

Consider the following example, which has two methods that add numbers of different type:

Instead of defining two methods that should do the same thing, it is better to overload one.

In the example below, we overload the plusMethod method to work for both int and double:

Overloading Rules

  • Methods must have the same name
  • They must differ in number of parameters, type of parameters, or both
  • Return type alone is not enough to distinguish overloaded methods
  • Overloading is resolved at compile time (static dispatch)
Java reference fragmentJAVA
// Overloaded area() — same name, different parameters
static double area(double r) {
    return Math.PI * r * r;          // circle
}
static double area(double w, double h) {
    return w * h;                    // rectangle
}
static double area(double b, double h, boolean triangle) {
    return 0.5 * b * h;             // triangle
}

// Java picks the right version automatically
System.out.println(area(5.0));           // 78.54 (circle)
System.out.println(area(4.0, 6.0));      // 24.0  (rectangle)

When to Use Method Overloading

  • When the same logical operation should work on different data types (e.g., add(int) vs add(double))
  • When varying numbers of arguments make sense for the same action (e.g., draw(radius) vs draw(width, height))
  • To provide convenience versions of a method with sensible defaults
  • Avoid overloading when the methods do fundamentally different things — use distinct names instead

Note: Multiple methods can have the same name as long as the number and/or type of parameters are different.

03 / Read and predict

A complete runnable example

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Area.of(4));
        System.out.println(Area.of(4, 3));
    }
}
class Area {
    static int of(int side) { return side * side; }
    static int of(int width, int height) { return width * height; }
}
Predict first. Then reveal the expected output / வெளியீடு
16
12

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

Make it your own

Practice challenge

Add an overload for a double side length and call it with 2.5.

Reveal the answer and reasoning / விடை

A declaration such as static double of(double side) { return side * side; } yields 6.25.

One mistake to avoid: Changing only int to double in the return type does not create a valid overload.

Your learning checkpoint

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.