Method Overloading
Overloading means several methods share one name but take different inputs — Add(int, int) and Add(double, double). The compiler picks the right one. It is compile-time polymorphism.
On this page · 5 min · Beginner
On this page
- In simple words
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
In simple words#
A “print” button that prints a page, a photo or a PDF — same name, different input. The compiler picks the version that matches what you pass.
- Overloading
- Same name, different parameter lists.
- Signature
- Method name + parameter types.
- Compile-time
- The choice is made when compiling.
Learning objectives#
- Define method overloading
- Write overloads in C#
- Know why JavaScript has no real overloading
- Achieve the same effect in JavaScript with defaults, rest parameters and type checks
Explanation#
In C#, methods can share a name if their parameter lists differ in number, type or order. The compiler chooses the overload from the arguments at compile time.
The return type alone does not count: int Parse(string) and double Parse(string) cannot both exist.
JavaScript has no overloading. If you declare add twice in a class, the second simply replaces the first.
JavaScript code gets the same flexibility in one method: default parameters (currency = "INR"), rest parameters (...parts) and checks on typeof or Array.isArray.
Overloading is for the same operation on different inputs. If the behaviour is really different, give it a different name.
Syntax#
public static class Formatter
{
public static string Price(decimal amount) => Price(amount, "INR");
public static string Price(decimal amount, string currency) => quot;{currency} {amount:F2}";
public static string Join(params string[] parts) => string.Join(" / ", parts);
public static string Describe(int n) => quot;number {n}";
public static string Describe(string s) => quot;text \"{s}\"";
}
Code example: Why JavaScript needs another way#
class Calculator {
add(a, b) { return a + b; }
add(a, b, c) { return a + b + c; } // replaces the first add!
}
console.log(new Calculator().add(1, 2));
// The JavaScript way: one flexible method per operation
class Formatter {
static price(amount, currency = 'INR') {
return `${currency} ${amount.toFixed(2)}`;
}
static join(...parts) {
return parts.join(' / ');
}
static describe(value) {
if (Array.isArray(value)) return `list of ${value.length}`;
if (typeof value === 'number') return `number ${value}`;
if (typeof value === 'object' && value !== null) return `object with ${Object.keys(value).length} keys`;
return `text "${value}"`;
}
}
console.log(Formatter.price(99.5));
console.log(Formatter.price(10, 'USD'));
console.log(Formatter.join('a', 'b', 'c'));
console.log(Formatter.describe([1, 2, 3]));
console.log(Formatter.describe(42));
console.log(Formatter.describe({ id: 1, name: 'Pen' }));
console.log(Formatter.describe('hi'));
NaN INR 99.50 USD 10.00 a / b / c list of 3 number 42 object with 2 keys text "hi"
Step-by-step explanation#
- The second add replaces the first, so add(1, 2) computes 1 + 2 + undefined = NaN.
- price uses a default parameter instead of two overloads.
- join uses a rest parameter to accept any number of arguments.
- describe checks the type at runtime — what C# would do with separate overloads at compile time.
Real-world use case#
Console.WriteLine in C# has 18 overloads — for int, string, bool, object and more — so you can print anything with one familiar name. ASP.NET Core’s Ok(), Ok(value) and Ok(object) work the same way.
Common mistakes#
No error is shown — the later one silently wins. Use one method with defaults or rest parameters.
Error CS0111: type already defines a member with the same parameter types. Change the parameters or the name.
Overloads like F(int, double) and F(double, int) make F(1, 1) ambiguous. Keep overload sets simple.
Best practices#
- Overload only for the same behaviour on different inputs
- Have simpler overloads call the full one, so logic lives in one place
- In JavaScript, prefer default and rest parameters over type checks
- Use an options object when there are many optional parameters
Remember this#
Overloading = same name, different parameters, chosen at COMPILE time.
Return type alone does not make an overload.
JavaScript: last definition wins — use defaults and ...rest.
Practice exercise#
Your task: Write a JavaScript function area(a, b) that returns the area of a square when called with one number and of a rectangle when called with two.
Need a hint?
Show solution
function area(a, b = a) {
return a * b;
}
console.log(area(4)); // 16
console.log(area(4, 5)); // 20
Mini quiz#
Overloading is an example of…
Want more? Practise all OOP Concepts MCQs · OOP Concepts interview questions
My notes#
Saved in this browser.