Reference Manual • Chapter 3

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:

📺 Layman Analogy: Universal Remote Control

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]
                    
Architecture Specification 3.1: Virtual Method Table (VTable) Polymorphic Dispatch
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:

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

Exercise 3.1: Contrast method overriding (`override`) against method hiding (`new`) in C#. Show the runtime execution result when calling a hidden method through a base class reference.
Step-by-Step Reference Solution:
- 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.
Exercise 3.2: Explain the Liskov Substitution Principle (LSP). How do interfaces support LSP in enterprise C# architectures?
Step-by-Step Reference Solution:
- 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.