OOP Concepts · Polymorphism in Depth

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.

Beginner 5 min Lesson 13 of 38 Runnable examplesExamples use modern JavaScript (ES2022 private fields) and C# 13.
Before you start:Polymorphism
0/38 in OOP Concepts
On this page · 5 min · Beginner

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#

C# Illustrative
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#

JavaScriptWhy JavaScript needs another way Runnable
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'));
Expected output
NaN
INR 99.50
USD 10.00
a / b / c
list of 3
number 42
object with 2 keys
text "hi"
Opens this example in the full playground

Step-by-step explanation#

  1. The second add replaces the first, so add(1, 2) computes 1 + 2 + undefined = NaN.
  2. price uses a default parameter instead of two overloads.
  3. join uses a rest parameter to accept any number of arguments.
  4. 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#

Declaring the same method twice in JavaScript

No error is shown — the later one silently wins. Use one method with defaults or rest parameters.

Overloads that differ only by return type (C#)

Error CS0111: type already defines a member with the same parameter types. Change the parameters or the name.

Ambiguous overloads

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?
Give b a default value of a.
Show solution
JavaScript Runnable
function area(a, b = a) {
  return a * b;
}
console.log(area(4));    // 16
console.log(area(4, 5)); // 20
Opens this example in the full playground

Mini quiz#

Method Overloading quizMultiple choice Question 1 of 3

Overloading is an example of…

Score so far: 0/0

Want more? Practise all OOP Concepts MCQs · OOP Concepts interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.