Object-oriented programming · interview question
Explain the difference between method overloading and method overriding in C#. Give a short example of each.
0 words · aim for 40+
Answer and explanation
Correct answer
Overloading means several methods in the same type share a name but have different parameter lists, for example Add(int a, int b) and Add(double a, double b). The compiler picks one at compile time from the argument types, so it's compile-time (static) polymorphism.
Overriding means a derived class replaces a base-class virtual (or abstract) member with the same signature using override. For example, Shape.Area() is virtual and Circle overrides it. The method that runs is chosen at runtime from the object's actual type, which is runtime polymorphism. The return type can't differ only by return type for overloading, and overriding requires the base member to be virtual, abstract or override.
Key points:
- Overloading: same name, different parameter lists, in the same type
- Overload resolution happens at compile time
- Overriding: derived class replaces a virtual/abstract member with the same signature using override
- Override is resolved at runtime based on the object's actual type
- Gives a concrete example of each
Why
Interviewers want the compile-time vs runtime distinction and the keywords (virtual, override) plus a concrete example.
Test yourself on Object-oriented programming
A free test with instant, explained results. No account needed.