Overloading and overriding: concepts, difference and application

Overloading is several functions with the same name but different parameter lists in the same scope; overriding is a derived class redefining a base function with the exact same signature.

9 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Two words designed to confuse you

Overloading. Overriding. Two words that share nine letters, live in the same unit, and appear together in one exam question almost every year: "Differentiate function overloading and function overriding."

Mix them up in a viva and the follow-up questions get uncomfortable fast.

Here is the good news: you have already used both without naming them. The till's two bill() versions? Overloading. Student rewriting Person's discount()? Overriding.

Theory

Same counter vs family recipe

Overloading is one counter offering chai, chai(large), chai(large, extra sugar): one name, and the cashier picks the recipe by looking at what you asked for.

Overriding is the family's franchise branch taking the parent's standard thali recipe and replacing it with its own version, same dish name, same order slip, different kitchen. Which one you get depends on which branch serves you.

Theory

The two, formally

Function overloading: two or more functions in the same scope share a name but differ in their parameter list (number or types of parameters). The compiler selects by matching arguments.

double bill(double amt);

double bill(double amt, double disc);

Function overriding: a derived class redefines a base-class function using the same name AND same signature. The child's version replaces the parent's for that child.

class Person { virtual double discount(); };

class Student : public Person { double discount(); };

At a glance

Overloading vs overriding, the exam table

AspectOverloadingOverriding
SignatureMust DIFFER (params)Must MATCH exactly
WhereSame scope / classBase class vs derived class
Inheritance neededNoYes
ResolvedCompile timeRun time (with virtual)
Polymorphism familyCompile-time (static)Run-time (dynamic)

Quiz

In one class, a student writes: int calc(int a); and double calc(int a); expecting an overload. What happens?

  1. Compile error: a different return type ALONE cannot overload a function
  2. It works: return types make the signatures different
  3. It works, but only the int version is ever called
  4. Runtime error when calc is first called
Show the answer

Compile error: a different return type ALONE cannot overload a function

Overload resolution looks only at the parameter list. Both functions take (int), so to the compiler they are the same function declared twice with clashing return types: an error. This is the most-tested overloading fact in existence. To overload, change the parameters: calc(int) and calc(double) would be fine.

Think first

Classify these four

Label each as overloading or overriding before tapping:

1. Item() and Item(char n[], double p) in one class

2. Staff redefining Person's show() with the same signature

3. print(int) and print(char) in one file

4. Student redefining discount() from Person

Show the answer

1. Overloading (constructors with different parameter lists, the everyday application of overloading).

2. Overriding (base vs derived, same signature).

3. Overloading (same scope, different parameter types).

4. Overriding (the card family's whole point).

Quick test to reuse: different params in one place = overloading; same signature across a parent-child pair = overriding.

Watch out

The three mark-losers

1. Claiming overloading needs inheritance: it does not; it lives inside one scope.

2. Claiming return type alone can overload: it cannot; parameters must differ.

3. Forgetting virtual: overriding without it still compiles, but a base-class pointer will call the base version (early binding). Overriding only becomes run-time polymorphism when the base function is declared virtual.

Theory

Where each earns its keep

Overloading in the wild: constructors (default + parameterized + copy, one name, three forms) and operator overloading (total + item making sense for your own types).

Overriding in the wild: every framework you will ever use hands you a base class and expects you to override its hooks: Android's onCreate(), a game engine's update(). The card family's discount() is the same pattern in miniature.

Summary

Key takeaways

  • Overloading: same name, different parameter list, same scope, compile-time choice.
  • Overriding: same name, same signature, base vs derived, run-time choice when virtual.
  • Return type alone can NEVER overload a function.
  • Overloading needs no inheritance; overriding cannot exist without it.
  • Applications: overloaded constructors and operators; overridden hooks like discount().
  • Memory hook: one counter, many recipes vs franchise rewrites the family dish.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Polymorphism

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Overloading and overriding: concepts, difference and application · Object Oriented Programming and Data Structures (OOPs & D.S.) · Gri-Learn