12. Inheritance, Interfaces, and Polymorphism#
A payroll run, a checkout, or a reporting client should be able to work with many kinds of employee, payment, or report without a separate branch for each one. Building on composition, this chapter develops inheritance, virtual dispatch, abstract classes, and interfaces as ways to share behavior and let implementations stand in for one another. You will test substitution, convert safely between types, and choose a relationship from the requirements rather than inheriting merely to reuse code.
Inheritance: A derived class builds on a base class, chains its constructor, and preserves the base class’s promises.
objectand casting: Every class inherits fromobject, andis,as, and type patterns convert a general reference safely.Polymorphism: One call through a base-typed variable runs the override that belongs to the actual object, including inside a polymorphic list.
Abstract class: A general type that requires concrete derived behavior and can supply shared state or methods.
Interface: A named capability contract, such as
IComparable<T>, implemented by classes that need not share a base class.
After the core OOP sequence, apply these ideas through the database capstone or the AI application project extension. Databases add persistent records; the AI extension adds a reviewed suggestion through an interchangeable component. Either track is optional; CS1 is complete without them.
Chapter Video
C# for Beginners – Lesson 15: Inheritance, Polymorphism & Abstraction (26:48) by Mike’s Code.
Hosted on YouTube; property of its original creator, embedded here for educational reference only.
Learning Objectives
By the end of this chapter, you will be able to:
Derive a class from a base class, chain constructors with
base(...)so the base validation still runs, and useprotectedmembers where a derived class needs access.Override
virtualmembers and call the base implementation, and explain how hiding withnew,sealed, andabstractchange what a derived class may do.Override
ToStringfromobject, and convert a general reference safely withis,as, type patterns, and casts, handlingInvalidCastException.Trace a call in two steps: overload selection at compile time from the declared type, then override selection at runtime from the actual object.
Implement concrete classes for an abstract base class, and process a list of base-typed payments with polymorphic calls and type patterns, adding a new subtype without changing the client method.
Declare and implement interfaces, supply a collaborator through constructor injection, observe the collaboration with a test double, and implement more than one interface in one class.
Choose between an interface and an abstract class, and implement
IComparable<T>for sorting andIEquatable<T>for equality.Check every implementation against shared expectations, detect a derived class that breaks its base class’s contract, and choose composition when the relationship is has-a.
Supporting-reading outcome: read complete business designs that combine a base class, overriding, and an interface, and implement an abstract shape contract for area-based pricing.
Chapter Flow
Suggested three-meeting pace (115 minutes): read the relevant explanations and predict the selected example before class.
Meeting |
Minutes |
Guided focus |
|---|---|---|
1 |
40 |
12.1: base and derived classes, constructor chaining, overriding, hiding with |
2 |
35 |
12.2: virtual calls versus overload selection, abstract payment classes, and polymorphic lists with type patterns. |
3 |
40 |
12.3: declaring and implementing interfaces, constructor injection and test doubles, multiple interfaces, interface or abstract class, and |
Class uses selected traces, runs, and checks. Use the remaining worked examples for practice and reference. Supporting readings are selected separately; lab and homework follow the core lessons.
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Inheritance |
A relationship in which a derived class reuses and extends the members of a base class. |
2 |
Base class |
A class inherited by a derived class. |
3 |
Derived class |
A class extending one base class. |
4 |
Is-a relationship |
A relationship in which a derived type is a more specific kind of its base type and keeps the base type’s promises. |
5 |
Has-a relationship |
A relationship in which one object holds another as a collaborator; modeled with composition. |
6 |
Composition |
Building a type from collaborators rather than inheriting all of its behavior. |
7 |
Protected member |
A member visible to the declaring class and its derived classes, but not to other code. |
8 |
Virtual member |
An overridable member whose implementation can be selected through runtime dispatch. |
9 |
Override |
A replacement implementation for an inherited virtual or abstract member. |
10 |
Hiding |
Declaring a member with |
11 |
Sealed class |
A class that cannot be used as a base class. |
12 |
Abstract class |
A class that cannot be instantiated directly and may declare abstract members. |
13 |
Abstract method |
A method with no body that every concrete derived class must override. |
14 |
|
The root type from which every C# class inherits |
15 |
Upcast |
Treating a derived object through a base-typed variable, which is always safe. |
16 |
Downcast |
Converting a base-typed reference to a derived type, which can fail at runtime. |
17 |
Type pattern |
A test such as |
18 |
Runtime dispatch |
Selecting an override according to the actual object rather than the declared type. |
19 |
Overload |
A same-name method with a different parameter list, selected at compile time. |
20 |
Runtime polymorphism |
One call running different implementations depending on the actual object. |
21 |
Polymorphic collection |
A list declared with a base type, such as |
22 |
Substitution |
Using an implementation through a general type while preserving client expectations. |
23 |
Liskov substitution principle |
The rule that a derived class must be usable anywhere its base class is expected without breaking the base class’s contract. |
24 |
Behavioral contract |
The observable promises that a client relies on across interchangeable implementations. |
25 |
Interface |
A named capability contract with no instance state. |
26 |
Implements |
A class implements an interface when it supplies every member the interface declares. |
27 |
Constructor injection |
Supplying a collaborator when constructing its client. |
28 |
Delegation |
Passing a task to a collaborator object instead of performing it directly. |
29 |
Test double |
A controlled replacement used to observe or check collaboration. |
30 |
Default interface member |
An interface member that supplies a body, used sparingly to extend an interface. |
31 |
|
A library interface whose |
32 |
|
A library interface whose |