LearnThatStack Ace your next interview
C# · question
Question 6 of 112

Explain method overriding vs method overloading.

beginner
← All C# questions
Re-explain

Overloading gives several methods one name with different parameters, and the compiler picks one at compile time. Overriding replaces a base class method in a derived class, and the runtime picks one based on the object's actual type.

The compiler chooses an overload from the argument types, so overloading is compile-time polymorphism. Overloads can live in the same class, like the two Add methods below. The return type alone cannot tell overloads apart.

Overriding needs virtual or abstract on the base method and override in the derived class. A Shape variable holding a Circle runs Circle.GetArea, so overriding is runtime polymorphism.

Use overloads for the same operation on different inputs. Use overriding to change behavior in a subclass.

// Overloading
public class Calculator
{
    public int Add(int a, int b) { return a + b; }
    public double Add(double a, double b) { return a + b; }
}
// Overriding
public class Shape
{
    public virtual double GetArea() { return 0; }
}
public class Circle : Shape
{
    private double radius;
    public override double GetArea() { return Math.PI * radius * radius; }
}
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • If Circle declared GetArea with new instead of override, what would a Shape variable holding a Circle run? Should explain hiding versus overriding, and say which type decides the call when new is used: the declared type or the actual type.
  • Given Add(int, int) and Add(double, double), which one does Add(1, 2.5) call, and why? Should walk through overload resolution and the implicit int to double conversion, and say when two equally good matches make a call ambiguous.
  • Why can't the compiler tell overloads apart by return type alone? Should point out that a caller can ignore or convert the result, so the call itself gives the compiler nothing to choose by.
  • How does an override reuse the base class logic instead of replacing it completely? Should name base.GetArea() and give a case like adding validation or logging around the parent's behavior.
  • How would you stop a further subclass from overriding Circle.GetArea? Should name sealed override and note that sealed only applies to a method that already overrides.

What you can say

  1. Overloading means one method name with different parameter lists, and overriding means a derived class replaces a base class method.
  2. With overloading, the compiler picks the version at compile time from the argument types, so it's compile-time polymorphism.
  3. A Calculator can have one Add for two ints and another for two doubles in the same class, but return type alone can't tell overloads apart.
  4. Overriding needs virtual or abstract on the base method and override on the derived one.
  5. Then the runtime decides, so a Shape variable holding a Circle runs Circle's GetArea, and that's runtime polymorphism.
  6. I use overloads when only the inputs change, and I override only when a subclass has to behave differently, not just accept different arguments.

Weak answers to avoid

  • Swaps compile-time and runtime This is the core of the question. Overloads are resolved at compile time from argument types, and overrides at runtime from the object's actual type.
  • Says return type can tell overloads apart int Get() and string Get() in one class won't compile. Say plainly that overloads must differ in their parameters.
  • Leaves out virtual and override Without virtual, abstract or override, a derived method with the same name only hides the base one, and a Shape variable still runs Shape.GetArea. Name the keywords.
  • Recites definitions with no example "Same name, different behavior" fits both. Show Add(int, int) next to Add(double, double), then a Shape variable holding a Circle.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the C# cheatsheet.

← Back to all C# questions
Pro · $10/mo

98 of 112 C# answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime