Inheritance & Polymorphism
Inheritance, interfaces, virtual/override, method overloading, and why C# has no multiple class inheritance.
6 questions
JuniorTheoryVery commonHow does class inheritance work in C#, and how many base classes are allowed?
How does class inheritance work in C#, and how many base classes are allowed?
Inheritance lets a derived class extend a single base class with class D : B, automatically reusing the base's accessible members and adding or specializing its own. C# permits only single class inheritance — a class has at most one direct base — though it can implement many interfaces. Every class ultimately derives from System.Object.
Common mistakes
- ✗Thinking C# allows multiple base classes like
class D : B1, B2 - ✗Believing inherited base members must be re-declared in the derived class to be usable
- ✗Not knowing every class ultimately derives from
System.Object
Follow-up questions
- →How does a derived class reach a base member that it has shadowed?
- →If C# forbids multiple class inheritance, how do you combine several type contracts?
JuniorTheoryCommonWhat is an interface in C#, and how does a class relate to one?
What is an interface in C#, and how does a class relate to one?
An interface is a contract: a set of member signatures (methods, properties, events) a type promises to provide, with no instance state. A class declares it implements the interface and must supply every member; those members are implicitly public. A class may implement many interfaces, and code can treat any implementer through the interface type.
Common mistakes
- ✗Thinking an interface carries implementation or instance state like a base class
- ✗Believing a class may implement only one interface
- ✗Marking interface member implementations
publicredundantly orprivateby mistake
Follow-up questions
- →How does explicit interface implementation differ from implicit, and why use it?
- →What does a default interface method add, and what does it not change?
MiddleTheoryCommonWhat is the difference between method overloading and method overriding in C#?
What is the difference between method overloading and method overriding in C#?
Overloading defines several methods with the same name but different parameter lists in scope; the compiler picks one by the argument types at COMPILE time. Overriding replaces a base virtual method in a derived class with a matching signature, resolved at RUNTIME by the object's actual type. They are unrelated mechanisms: one selects among names, the other swaps a virtual implementation.
Common mistakes
- ✗Conflating overloading (compile-time, by parameters) with overriding (runtime, by type)
- ✗Thinking methods can be overloaded by return type alone
- ✗Believing overloading requires
virtual/overrideor a class hierarchy
Follow-up questions
- →Can you overload two methods that differ only by
refversusout? - →Why does overload resolution happen before runtime dispatch when both apply?
MiddleTheoryCommonHow do virtual/override differ from new when a derived class redefines a method?
How do virtual/override differ from new when a derived class redefines a method?
Marking a base method virtual and the derived one override enables runtime (dynamic) dispatch: a call through a base reference runs the derived version, chosen by the object's actual type. new instead hides the base member with compile-time binding — which method runs depends on the static type of the reference, so a base-typed reference still runs the base method.
Common mistakes
- ✗Believing
newgives polymorphism rather than compile-time hiding by static type - ✗Forgetting the base method needs
virtualbefore a derived method canoverrideit - ✗Expecting a base-typed reference to a
new-hidden method to run the derived version
Follow-up questions
- →What does the compiler warn about if you hide a base method without writing
new? - →How does
sealed overridechange what a further-derived class can do?
MiddleTheoryRareDoes C# support multiple inheritance, and how do you combine multiple types?
Does C# support multiple inheritance, and how do you combine multiple types?
C# forbids multiple class inheritance — a class has at most one base — to avoid the diamond problem of ambiguous inherited implementations. A class can, however, implement any number of interfaces. Since interfaces declare contracts (and need no shared implementation state), that is how C# achieves multiple inheritance of TYPE without the ambiguity of multiple inherited code.
Common mistakes
- ✗Thinking C# allows multiple base classes if the diamond is resolved leftmost
- ✗Believing a class may implement only one interface
- ✗Not seeing interfaces as C#'s answer to multiple inheritance of type
Follow-up questions
- →If two implemented interfaces declare the same method, how does the class satisfy both?
- →How do default interface methods reintroduce a limited diamond problem?
SeniorTheoryRareWhat does sealed do on a class versus on an overridden method in C#?
What does sealed do on a class versus on an overridden method in C#?
A sealed class cannot be inherited at all — it is a leaf in the hierarchy. sealed override on a method keeps the override but stops any further-derived class from overriding it again, capping that virtual chain. Sealing signals intent (this type/override is final) and lets the JIT devirtualize calls on a sealed type, since no overriding subtype can exist.
Common mistakes
- ✗Thinking a
sealedclass can still be inherited or cannot be instantiated - ✗Believing
sealed overrideremoves the override instead of capping further overriding - ✗Missing that sealing enables JIT devirtualization of virtual calls
Follow-up questions
- →At what point in a virtual chain can you first apply
sealed override? - →How does sealing a class enable the JIT to inline a previously virtual call?