Inheritance & Polymorphism
Inheritance lets one class extend another: a derived class reuses the base's accessible members and adds or specializes its own. Polymorphism is the other half of the same idea: code written against a base type dispatches, at runtime, to the behavior of the actual derived object. Together they let you build hierarchies where the common part lives in the base class and the differences live in the derived ones.
In C# this model has strict rules, and interview questions cluster around them. A class inherits at most one base class (single inheritance) but implements any number of interfaces. A method takes part in polymorphism only if it is virtual (or abstract) in the base and override in the derived class; new instead of override is not polymorphism — it hides a member by the reference's static type. Overloading and overriding are entirely different mechanisms: the compiler picks the first by parameters, the runtime picks the second by the object's type. The full map lives in the layers below.
Topic map
- Inheritance —
class D : B, reusing members, single inheritance, and theSystem.Objectroot. - Interfaces — a contract of signatures with no state, implicitly
publicmembers, many interfaces per class. - Virtual Methods —
virtual/abstractand dynamic dispatch by the object's actual type. - Method Overriding —
overrideversusnew: true polymorphism versus hiding by static type. - Method Overloading — one name, different parameters; resolved at compile time, not to be confused with overriding.
- Multiple Inheritance — one base class, many interfaces, and the diamond problem.
Common traps
| Mistake | Consequence |
|---|---|
Assuming C# allows class D : B1, B2 | Does not compile — multiple class inheritance is forbidden; combine several contracts with interfaces |
Thinking new gives polymorphism | new only hides a member by static type; a base-typed reference runs the base method, not the derived one |
Forgetting virtual/abstract on the base method | The derived override will not compile — you can only override a virtual member |
| Confusing overloading with overriding | Overloading is resolved by parameters at compile time; overriding by the object's actual type at runtime |
| Trying to overload by return type alone | Does not compile — the return type is not part of the signature for overload resolution |
| Thinking an interface carries state or a ready implementation | An interface is a contract of signatures; the class must supply each member's body, and those members are implicitly public |
Expecting a sealed class to still be inheritable | A sealed class is a leaf; inheritance is forbidden, and the JIT can devirtualize calls on it |
Interview relevance
Inheritance and polymorphism come up on almost every C# interview — but the check is your dispatch model, not the syntax. A candidate who explains polymorphism as "a call through a base-typed reference picks the method by the object's actual type" immediately stands out from "well, the subclass overrides the method".
Typical checks:
- Single class inheritance versus multiple interface implementation, and why.
- The difference between
virtual/override(runtime dispatch) andnew(hiding by static type). - Overloading (compile time, by parameters) versus overriding (runtime, by type) — unrelated mechanisms.
- That an interface is a contract with no state, and its members on a class are implicitly
public. - The diamond problem and why C# solves it by forbidding multiple class inheritance.
Common wrong answer: "new and override are the same thing, both re-define the method". That opens the discussion that override enables dynamic dispatch while new binds to the reference's static type — so the same base-typed variable gives a different result depending on which keyword redefined the method.