Stop 03 · Control Flow, Methods & ArraysTopics · 6/8

Method Overloading

Part of the Java Roadmap

In short

println works with ints, doubles, Strings and booleans, all under one name. That is method overloading, and it lets you offer one friendly name for several closely related actions.

Overloading means several methods share a name but differ in parameter types or count. The compiler picks the right one at compile time. A different return type alone is not enough.

The jaadu sits at a reception desk with one big telephone connected by cords to three different office doors behind it.

Think of it like this

One helpline, many options

A bank has a single helpline number. Press 1 with your card number, or 2 with your account number and PIN, and the right desk picks up. Same number, different inputs, different desk. Overloaded methods share one name, and Java picks the version whose parameters match what you pass.

Watch it happen

Which area() gets called?

area(...)
area(double r): circle
area(double l, double w): rectangle
area(int side): square

1/4Three methods share the name area but have different parameter lists.

The big ideas

What you need to know

Different parameters

Overloads must differ in the number, types or order of parameters.

Return type alone is not enough

int total() and double total() with the same parameters will not compile.

Chosen at compile time

The compiler picks the best match from the argument types you write. This is static polymorphism.

Widening counts

If no exact match exists, Java can widen an int to long or double to find one.

See it in code

Walk through the code

java
1public class Fare {2 3    static double fare(int km) {4        return 30 + km * 12;5    }6 7    static double fare(int km, boolean isNight) {8        double base = fare(km);9        return isNight ? base * 1.25 : base;10    }11 12    static double fare(double km, String vehicle) {13        double rate = vehicle.equals("AUTO") ? 15 : 22;14        return 30 + km * rate;15    }16 17    public static void main(String[] args) {18        System.out.println(fare(10));            // 150.019        System.out.println(fare(10, true));      // 187.520        System.out.println(fare(7.5, "AUTO"));   // 142.521    }22}

1/4The simple version: base ₹30 plus ₹12 per km.

Three ways to calculate a ride fare, all called fare. The arguments decide which one runs.

Save yourself the pain

Common mistakes

Oops

Trying to overload by return type only: int sum(int a, int b) and long sum(int a, int b).

Do this instead

Change the parameters, or give the methods different names.

Oops

Confusing overloading with overriding.

Do this instead

Overloading: same name, different parameters, same class, picked at compile time. Overriding: a subclass replaces a parent method, picked at runtime.

Your mission

Overload a printer

~15 min

Quick check

Which pair is a valid overload?

Go deeper

  1. 1Write add(int, int), add(double, double) and add(int, int, int).
  2. 2Try overloading by return type only and read the compile error.
  3. 3Look at how System.out.println is overloaded for many types.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge