Java Notes
Learn method overloading in Java — same method name with different parameters, how Java picks the right version, what you can and cannot overload, and common mistakes.
Method overloading means a class has multiple methods with the same name but different parameter lists. Java looks at the arguments you pass and automatically picks the right version.
This is compile-time polymorphism — the decision is made at compile time, not while the program runs.
Why Overloading Exists
Without overloading you would write:
void printInt(int x) { System.out.println(x); }
void printDouble(double x) { System.out.println(x); }
void printString(String x) { System.out.println(x); }With overloading all three become print(...) — one name, three behaviours.
Basic Overloading Example
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
- Four methods named
print. Java tells them apart by parameter type or count. p.print(10)— matchesprint(int).p.print(3.14)— matchesprint(double).p.print("Java")— matchesprint(String).p.print(5, 8)— two ints, matchesprint(int, int).
Java resolves this at compile time — it knows which method to wire up before running the program.
What Makes Methods Different Enough to Overload
You can overload by changing:
- Number of parameters —
add(int a)vsadd(int a, int b) - Type of parameters —
add(int a, int b)vsadd(double a, double b) - Order of parameter types —
add(int a, double b)vsadd(double a, int b)
You cannot overload by changing only the return type:
// COMPILE ERROR — Java cannot tell these apart from the call site
int getValue() { return 1; }
double getValue() { return 1.0; } // error: getValue() is already definedWhen you write getValue(), Java has no way to know which return type you want. So overloading by return type alone is not allowed.
Real Example: Area Calculator
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
One name area handles four different shapes. The caller just passes the right arguments.
Type Promotion in Overloading
When an exact match is not found, Java automatically widens the type to find the closest match.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Java's promotion order: byte → short → int → long → float → double. If no exact match exists, Java finds the smallest type that can hold the value.
Constructor Overloading
Constructors can be overloaded exactly like methods:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Three constructors for the same class. Java picks the right one based on what you pass to new Config(...).
Common Mistake 1: Trying to Overload by Return Type Only
// COMPILE ERROR
int process() { return 1; }
String process() { return "one"; } // duplicate method
// FIX: use different parameter types or counts
int processAsInt() { return 1; }
String processAsString() { return "one"; }Common Mistake 2: Overloading vs Overriding Confusion
public class Parent {
void show(int x) { System.out.println("Parent: " + x); }
}
public class Child extends Parent {
// This is OVERLOADING — different parameter type
void show(double x) { System.out.println("Child double: " + x); }
// This is OVERRIDING — same signature as Parent
@Override
void show(int x) { System.out.println("Child int: " + x); }
}Overloading can happen within one class or across parent/child. Overriding specifically means a child redefines a method with the same signature.
Practice Task
Task: Create a class StringUtils with overloaded methods all named repeat:
repeat(String s, int times)— returnssrepeatedtimestimes (e.g.,"ab", 3 →"ababab")repeat(char c, int times)— returnscrepeatedtimestimesrepeat(String s)— repeatssexactly 2 times (calls the first method)
Hint: The third version can call the first using return repeat(s, 2). Build the string using a loop or StringBuilder.
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: su.repeat("ab", 3) goes to the two-argument version. su.repeat('*', 5) goes to the char version. su.repeat("ha") goes to the one-argument version which delegates to the two-argument version. Same name, three different paths.
Continue the lesson path