Hey!
Let me ask you something before we start.
You go to a coffee shop. You order a coffee. The barista asks — small, medium, or large? With milk or without? Hot or cold?
Same order — "one coffee." But the result is different based on what details you give.
That is method overloading in Java.
Same method name. Different inputs. Different results.
By the end of this blog — you will understand methods, method overloading, and why Java developers call it compile time polymorphism.
Let us start.
1. What Is a Method?
A method is a block of code that does a specific job. You write it once and call it whenever you need it.
Think of it like a washing machine. You press the button — it washes. You do not care how it works inside. You just press and get the result.
public static void greet() {
System.out.println("Hello! Welcome.");
}
You write this once. Every time you need to greet — you just call greet(). No need to write the same print statement again and again.
2. Structure of a Method
public static int add(int a, int b) {
return a + b;
}
Let us read this piece by piece.
public — this method is accessible from anywhere.
static — you can call this method without creating an object.
int — the return type. This method gives back an integer value.
add — the method name.
(int a, int b) — parameters. The inputs this method needs to do its job.
return a + b — the result sent back to whoever called the method.
3. Calling a Method
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(10, 20);
System.out.println("Sum: " + result); // Sum: 30
}
}
add(10, 20) — calling the method with values 10 and 20.
The method runs, adds them, and returns 30. That 30 is stored in result.
4. Types of Methods
There are four simple variations.
No parameter, no return
public static void sayHello() {
System.out.println("Hello!");
}
Takes nothing. Returns nothing. Just runs and does its job.
With parameters, no return
public static void greet(String name) {
System.out.println("Hello, " + name);
}
Takes a name. Prints it. Returns nothing — so return type is void.
No parameter, with return
public static String getAppName() {
return "MyApp";
}
Takes nothing. Returns a String value.
With parameters, with return
public static int multiply(int a, int b) {
return a * b;
}
Takes two numbers. Returns their product.
Quick question for you.
If a method has void as the return type — what does that mean?
It means the method does not return anything. It just performs an action — printing, saving, updating — and finishes. No value comes back.
5. Why Do We Need Methods at All?
Without methods — you repeat the same code everywhere.
// Without methods — repeated code
System.out.println("Area: " + (5 * 3));
System.out.println("Area: " + (8 * 4));
System.out.println("Area: " + (6 * 7));
With a method — write once, use anywhere.
public static int area(int length, int width) {
return length * width;
}
System.out.println("Area: " + area(5, 3)); // 15
System.out.println("Area: " + area(8, 4)); // 32
System.out.println("Area: " + area(6, 7)); // 42
Clean. Reusable. If the logic changes — you update one place. Not three.
6. Now — What Is Method Overloading?
Here is a situation.
You are building a calculator. You need to add numbers.
Sometimes the user passes two integers.
Sometimes they pass three integers.
Sometimes they pass decimal numbers.
Without overloading — you would name them differently.
public static int addTwoInts(int a, int b) { ... }
public static int addThreeInts(int a, int b, int c) { ... }
public static double addTwoDoubles(double a, double b) { ... }
That is messy. Three different names for something that is essentially the same action — adding numbers.
Method overloading lets you use the same name for all of them.
public static int add(int a, int b) { ... }
public static int add(int a, int b, int c) { ... }
public static double add(double a, double b) { ... }
Same name add. Different parameters. Java figures out which one to call based on what you pass.
That is method overloading.
7. How Does Overloading Work? — The Rules
For overloading to work — the methods must differ in at least one of these ways.
Number of parameters
public static int add(int a, int b) { ... } // 2 params
public static int add(int a, int b, int c) { ... } // 3 params
Type of parameters
public static int add(int a, int b) { ... } // int params
public static double add(double a, double b) { ... } // double params
Order of parameters
public static void display(int a, String b) { ... }
public static void display(String a, int b) { ... }
Quick question for you.
Can you overload a method by just changing the return type?
public static int getValue() { return 1; }
public static double getValue() { return 1.0; }
No. This is not valid overloading. Java does not allow this. The return type alone is not enough to distinguish two methods. Java needs to know which one to call at the point you call it — and at that point, it only sees the method name and arguments, not the return type.
8. A Full Overloading Example
public class Calculator {
// Two integers
public static int add(int a, int b) {
System.out.println("Adding two integers");
return a + b;
}
// Three integers
public static int add(int a, int b, int c) {
System.out.println("Adding three integers");
return a + b + c;
}
// Two doubles
public static double add(double a, double b) {
System.out.println("Adding two doubles");
return a + b;
}
// int and double
public static double add(int a, double b) {
System.out.println("Adding int and double");
return a + b;
}
public static void main(String[] args) {
System.out.println(add(10, 20)); // calls first method
System.out.println(add(10, 20, 30)); // calls second method
System.out.println(add(1.5, 2.5)); // calls third method
System.out.println(add(5, 3.14)); // calls fourth method
}
}
Output:
Adding two integers
30
Adding three integers
60
Adding two doubles
4.0
Adding int and double
8.14
Same name add. Four different versions. Java picks the right one based on the arguments you pass. You do not have to think about which version to call — you just call add() and Java handles it.
9. Why Is Method Overloading Called Compile Time Polymorphism?
This is the question most beginners skip. But it is important to understand.
Let us break it word by word.
Polymorphism means — one name, many forms. The word comes from Greek — poly means many, morph means form.
add is one name. But it has four forms. Each form behaves differently based on the input.
That is polymorphism.
Compile time means — the decision of which form to use is made at compile time. Not when the program runs.
When you write add(10, 20) — the Java compiler looks at your arguments right there and says — "two integers. I know exactly which add method to call. I will lock that in now."
The decision is made before the program even runs.
You write: add(10, 20)
↓
Compiler checks: two int arguments
↓
Compiler says: call add(int a, int b)
↓
Decision is locked at compile time
↓
Program runs — already knows which method to use
This is why it is called compile time polymorphism. Also called static polymorphism or early binding — because the binding between the method call and the method definition happens early, at compile time.
Quick question for you.
If the decision is already made at compile time — does that make the program faster at runtime?
Yes. Because at runtime — Java does not need to figure out which method to call. That work is already done. The program just executes the pre-decided method directly.
10. Real World Example — print() Method
You have been using method overloading without realizing it.
System.out.println() is an overloaded method.
System.out.println(42); // int version
System.out.println(3.14); // double version
System.out.println("Hello"); // String version
System.out.println(true); // boolean version
System.out.println('A'); // char version
Five different types. Same method name. Java picks the right version based on what you pass.
You never had to write printInt() or printString() or printDouble(). Just println. That is the clean power of method overloading.
11. Method Overloading vs Method Overriding
Beginners often confuse these two. They sound similar but are completely different.
| Method Overloading | Method Overriding | |
|---|---|---|
| Where | Same class | Parent and child class |
| Method name | Same | Same |
| Parameters | Must be different | Must be same |
| Decided at | Compile time | Runtime |
| Also called | Compile time polymorphism | Runtime polymorphism |
| Purpose | Same action, different inputs | Change inherited behaviour |
Overloading — same class, different inputs, decided at compile time.
Overriding — different classes (parent/child), same inputs, decided at runtime.
We will cover overriding in a separate blog. For now — just know they are not the same thing.
12. Common Mistakes to Avoid
Trying to overload with only return type
public static int getValue() { return 1; }
public static double getValue() { return 1.0; } // Error
Not valid. Return type alone cannot differentiate overloaded methods.
Confusing overloading with overriding
Overloading is in the same class. Overriding involves inheritance. Do not mix them up.
Too many overloaded versions
public static int add(int a) { ... }
public static int add(int a, int b) { ... }
public static int add(int a, int b, int c) { ... }
public static int add(int a, int b, int c, int d) { ... }
For too many parameters — consider using an array or a list instead. Overloading is useful but do not overdo it.
Quick Summary — 5 Things to Remember
A method is reusable code — write once, call whenever needed. Keeps your code clean and DRY (Do not Repeat Yourself).
Method overloading — same method name, different parameters. Java picks the right version at compile time based on the arguments.
Overloading rules — methods must differ in number of parameters, type of parameters, or order of parameters. Return type alone is not enough.
Compile time polymorphism — one name, many forms, decision made at compile time. Also called static binding or early binding.
You already use overloading —
System.out.println()is a real-world example. Same name. Works with int, double, String, boolean, char.
Methods make your code organised. Method overloading makes it clean and natural to use.
Instead of remembering five different method names for the same kind of action — you just remember one. Java does the rest.
Try writing the Calculator class from section 8. Add one more overloaded version — maybe one that takes three doubles. Call it and see Java pick it correctly.
Then try the mistake on purpose — change only the return type and see the error Java gives you.
That hands-on experience will make everything click.
If you have a question — drop it in the comments below.
Thanks for reading. Keep building.
Top comments (0)