Overloading vs overriding
28 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Overloading gives one method name several parameter lists. Overriding gives a child class its own version of an inherited method with the same parameters. The names sound alike, but they solve different problems and are decided at different moments.
Overloading in one line
Method overloading is defining several methods with the same name but different parameter lists (number, types or order of parameters). The compiler picks the version from the arguments of each call.
You learned it in Method overloading (opens in a new tab): more ways to call one action, like println for an int, a double or a String. A bill can be made from a price, or a price and a quantity.
Example 1 · Overloading: three ways to make a bill
class Bill { int total(int price) { return price; }
int total(int price, int qty) { return price * qty; }
int total(int price, int qty, int discountPercent) { int amount = price * qty; return amount - amount * discountPercent / 100; }}
public class BillDemo { public static void main(String[] args) { Bill b = new Bill(); System.out.println(b.total(120)); System.out.println(b.total(120, 3)); System.out.println(b.total(120, 3, 10)); }}120 360 324
What this code does
- All three methods sit in one class with the name
total; only the parameter lists differ. - For each call the compiler counts the arguments and picks the matching version. No inheritance is needed.
Side by side
Overloading puts versions next to each other; overriding replaces a version in a child. One printer family can use both, and the picture shows who decides each call.
Example 2 · Both in one program
class Printer { void print(String doc) { System.out.println("B/W: " + doc); }
void print(String doc, int copies) { System.out.println("B/W: " + doc + " x" + copies); }}
class ColorPrinter extends Printer { @Override void print(String doc) { System.out.println("Colour: " + doc); }}
public class PrinterDemo { public static void main(String[] args) { Printer p = new ColorPrinter(); p.print("Admit card"); p.print("Admit card", 2); }}Colour: Admit card B/W: Admit card x2
What this code does
- Inside
Printer, the twoprintmethods are overloads.ColorPrinteroverrides onlyprint(String). - For each call the compiler first picks the overload by the arguments; then the object, a
ColorPrinter, decides which version of that overload runs. - The two-argument call has no child version, so the inherited black-and-white one runs.
Read each row as "overloading / overriding":
- Where: one class, or a child adding a version / always a parent and a child.
- Parameters: must differ / must be exactly the same.
- Return type and access: anything / same return type (or a subtype) and the same or wider access.
- Decided: at compile time from the arguments / at run time from the object.
Return type alone is not enough
Java picks an overload from the arguments only, never from the return type. So two methods with the same name and parameter types cannot live in one class, whatever they return.
Example 3 · Mistake: only the return type differs
public class ReturnOnly { static int total(int price) { return price; }
static double total(int price) { return price * 1.18; }
public static void main(String[] args) { System.out.println(total(100)); }}ReturnOnly.java:6: error: method total(int) is already defined in class ReturnOnly
static double total(int price) {
^
1 errorWhat this code does
- Both methods are
total(int). From the calltotal(100)Java could not tell which one you mean. - The error is reported on the second method, line 6, as soon as it repeats a signature that already exists.
The same error appears when only the parameter names differ: area(int side) and area(int length) are one method to Java. Fix it with a different parameter list or a different name, such as totalWithGst(int price).
A child can overload too
A child method with the inherited name but new parameters replaces nothing: it adds an overload. The child then has both versions, and each call picks by its arguments.
Example 4 · A child that overrides and overloads
class Payment { void pay(int amount) { System.out.println("Paid Rs. " + amount); }}
class UpiPayment extends Payment { @Override void pay(int amount) { System.out.println("UPI paid Rs. " + amount); }
void pay(int amount, String note) { pay(amount); System.out.println("Note: " + note); }}
public class BothDemo { public static void main(String[] args) { UpiPayment u = new UpiPayment(); u.pay(300); u.pay(150, "Canteen lunch"); }}UPI paid Rs. 300 UPI paid Rs. 150 Note: Canteen lunch
What this code does
pay(int)overrides the parent method.pay(int, String)is a new overload that exists only inUpiPayment.- Inside the overload,
pay(amount)has one argument, so it calls the one-parameterpay. That is not recursion.
Other confusions that come up in interviews:
static,privateandfinalmethods can be overloaded, but none of them can be overridden.maincan be overloaded, but the JVM starts only frommain(String[] args).- A child overload you meant as an override fails silently; @Override (opens in a new tab) turns it into an error.
Next step
Check your understanding
11 quick questions