Object-Oriented Design, Polymorphism & Interfaces
An authoritative reference manual on C# OOP principles, inheritance hierarchies, Virtual Method Tables (VTables), dynamic polymorphism, and contract interfaces.
Object-Oriented Programming (OOP) in C# provides structural paradigms for managing enterprise code complexity. This chapter examines dynamic method dispatch via VTables, abstract base templates, interface contracts, and SOLID principles.
3.1. Dynamic Polymorphism (`virtual` vs `override` vs `new` Shadowing)
Definition: Dynamic Polymorphism enables method execution to be resolved at runtime based on the actual type of the instantiated object rather than the compile-time variable reference.
Core Working Principles:
- Non-Virtual Default: C# methods are non-virtual (sealed) by default to enforce static invocation speed and prevent unintentional modification.
- `virtual` Keyword: Declares that a base class method allows polymorphic overriding in derived classes.
- `override` Keyword: Replaces the base class method implementation in the Virtual Method Table (VTable).
- `new` Keyword (Method Hiding): Hides the base class method without participating in runtime VTable polymorphic dispatch.
Imagine pressing the POWER button on a universal remote control.
Polymorphism (override): If the remote is pointed at a Sony TV, it invokes Sony's power protocol. If pointed at a Samsung TV, it invokes Samsung's power protocol. The remote caller sends one command, but the receiver executes its own specific behavior!
graph TD
Ref[Base Variable: Employee emp = new Manager()] -->|Call emp.CalculateBonus()| VTable{VTable Pointer Lookup}
VTable -->|Polymorphic Dispatch| MgrMethod[Manager.CalculateBonus() Override Method]
VTable -.->|Bypassed| EmpMethod[Employee.CalculateBonus() Base Method]
public class Employee {
public virtual void ProcessPay() => Console.WriteLine("Base Pay");
}
public class Manager : Employee {
public override void ProcessPay() => Console.WriteLine("Manager Pay + Bonus");
}
public class Contractor : Employee {
public new void ProcessPay() => Console.WriteLine("Contractor Hourly"); // Hides!
}
// Execution Behavior
Employee e1 = new Manager();
e1.ProcessPay(); // Outputs: "Manager Pay + Bonus" (Override)
Employee e2 = new Contractor();
e2.ProcessPay(); // Outputs: "Base Pay"! (Method Hiding ignores VTable!)
5-Marks Exam Scoring Points
- virtual enables polymorphic dispatch; override replaces base implementation in the VTable.
- new keyword hides base methods statically without participating in runtime polymorphic dispatch.
- C# methods are non-virtual by default to eliminate VTable lookup overhead unless explicitly opted-in.
3.2. Contract-Driven Design: Abstract Classes vs. Interfaces
Definition: Abstract Classes model core identity relationships ('is-a') with shared state, whereas Interfaces model capabilities ('can-do') without internal state fields.
Core Working Principles:
- Abstract Class: Class that cannot be instantiated directly. Serves as a base blueprint, supporting constructors, instance state fields, and protected members under single class inheritance.
- Interface (`interface`): Pure behavioral contract defining method signatures, properties, and events without instance fields. Classes can implement multiple interfaces.
- Default Interface Methods (C# 8+): Allows adding default method implementations to interfaces without breaking existing implementations.
| Design Axis | Abstract Class | Interface (interface) 🏆 |
|---|---|---|
| Inheritance Model | Single Class Inheritance (: BaseClass) |
Multiple Interface Implementation (: IFoo, IBar) |
| State & Fields | Can contain instance fields & state variables | Cannot hold instance state fields |
| Constructors | Supports protected constructors |
No constructors allowed |
| Relationship Type | High-coupling IS-A identity relationship | Decoupled CAN-DO behavioral contract |
public interface IPaymentGateway {
bool ProcessTransaction(decimal amount);
}
public abstract class BaseNotifier {
protected string SenderAddress;
public BaseNotifier(string address) => SenderAddress = address;
public abstract void SendAlert(string message);
}
5-Marks Exam Scoring Points
- Abstract classes model core IS-A identity hierarchies and share state; interfaces model CAN-DO contracts.
- C# limits classes to single class inheritance but allows unlimited interface implementations.
- Interfaces decouple concrete implementations from consumer code, enabling unit test mocking.
✍️ Chapter 3 Reference Exercises & Review Problems
- Method Overriding (`override`): Updates the Virtual Method Table (VTable). Calling the method via a base reference e.g. `Base b = new Derived()` dispatches to `Derived.Method()` at runtime.
- Method Hiding (`new`): Creates a new static method slot that does NOT update the VTable. Calling the method via a base reference `Base b = new Derived()` invokes `Base.Method()`, ignoring the derived implementation.
- Liskov Substitution Principle: Subtypes must be substitutable for their base types without altering application correctness.
- Role of Interfaces: Interfaces decouple implementation logic from consumers. By coding against interface abstractions (e.g. `IRepository`), any implementing class can be substituted at runtime (e.g. via Dependency Injection) without breaking client code.