Inheritance and virtual dispatch
Inheritance and virtual dispatch
Inheritance creates a class from another class. A derived class can reuse accessible members and specialize behavior. Polymorphism lets code work with a base type while the actual object supplies its overridden behavior. A shipping calculator can accept a general delivery type and still compute the correct fee for express delivery.
using System;Delivery delivery = new ExpressDelivery();Console.WriteLine(delivery.Fee());class Delivery{ public virtual int Fee() => 5;}class ExpressDelivery : Delivery{ public override int Fee() => 12;}The variable has type Delivery, but the object is ExpressDelivery. The virtual call reaches the override.
Expected output (reasoned, not executed):
12A base reference and a derived object
In class ExpressDelivery : Delivery, Delivery is the base class. ExpressDelivery is the derived class. A C# class has at most one direct base class. An implicit reference conversion allows Delivery d = new ExpressDelivery();. This does not create a second object or remove derived fields. The declared type controls what members can be selected at compile time. For a virtual member, the runtime chooses the most derived override of the selected virtual member for that object. A hidden member declared with new is not an override of that slot; a new virtual member starts a separate virtual dispatch chain.
Choose override, not hiding
Mark a base instance method virtual when derived types may replace its behavior. Use override with a compatible signature in a derived class. A nonvirtual member cannot be overridden. new on a member explicitly hides an inherited member; it does not replace that member’s virtual dispatch slot. Calling through a base reference can therefore produce a different result from calling through a derived reference. base.Method() inside an override calls the base implementation directly. Constructors are not inherited; a derived constructor can pass values to a base constructor. A sealed class cannot be extended.
using System;Delivery[] deliveries = { new Delivery(2), new ExpressDelivery(2) };foreach (Delivery delivery in deliveries) Console.WriteLine(delivery.Fee());class Delivery{ private readonly int _boxes; public Delivery(int boxes) => _boxes = boxes; public virtual int Fee() => 3 + _boxes;}class ExpressDelivery : Delivery{ public ExpressDelivery(int boxes) : base(boxes) { } public override int Fee() => base.Fee() + 7;}Both objects get the base constructor state. The express override adds seven to the base result. The sample assumes a nonnegative small box count.
Expected output (reasoned, not executed):
512using System;Derived specific = new Derived();Base general = specific;Console.WriteLine(specific.Label());Console.WriteLine(general.Label());class Base{ public string Label() => "Base";}class Derived : Base{ public new string Label() => "Derived";}Label is nonvirtual. The declared receiver type selects a different member; both references still point to the same object.
Expected output (reasoned, not executed):
DerivedBaseCommon mistakes
Do not declare a same-named method and assume it overrides the parent. Use an intentional virtual/override pair and test calls through both declared types. Avoid using inheritance only to share a helper; an unrelated type should not be forced into an is-a relationship.
Practice
Create Delivery with virtual Fee returning 4. Add WeekendDelivery returning base.Fee() + 3. Create a Delivery array containing one of each and print the fees. Then explain what would change if the derived method used new instead of override.
Predict the outcome before checking your work. The separate manual practice and quiz are staged, not automatically graded.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Inheritance and virtual dispatch — knowledge check
Apply the lesson concepts to the following scenarios.
Delivery d = new ExpressDelivery(); Fee is virtual and overridden. Which implementation runs?
What does assigning an ExpressDelivery to a Delivery variable copy?
What does base.Fee() do in the express override?
A nonvirtual Label is hidden using new. What selects the member in an ordinary call?
Which relationship is a good starting point for inheritance?