Abstract classes and design trade-offs
Abstract classes and design trade-offs
An abstract class is a class that cannot be created directly. It can contain shared code, constructor state, and members that concrete descendants must implement. Use one when related types need common implementation or invariants. When only a behavior contract is needed, an interface or a simple composed object is often enough.
using System;Formatter formatter = new UpperLabel("Shelf");Console.WriteLine(formatter.Render());abstract class Formatter{ private readonly string _prefix; protected Formatter(string prefix) => _prefix = prefix; public string Render() => _prefix + ": " + Format(); protected abstract string Format();}class UpperLabel : Formatter{ public UpperLabel(string prefix) : base(prefix) { } protected override string Format() => "READY";}Render provides the stable shared operation. Format is required specialization. UpperLabel supplies it; the private prefix is initialized by the base constructor.
Expected output (reasoned, not executed):
Shelf: READYShared structure with required specialization
An abstract method has a declaration ending in a semicolon, not a method body. A concrete derived class must implement inherited abstract members that remain unimplemented. An abstract class can have ordinary and virtual methods too. A protected constructor is available to derived constructors, but outside callers cannot directly instantiate the abstract class. Protected access is for derived code, with restrictions on which receiver can be used. Keep mutable implementation details private when possible.
Choose an abstraction based on the problem
Use an abstract class when several genuine subtypes share required initialization and a stable common operation. Use an interface when unrelated implementations should satisfy one behavior contract. Use composition to delegate a replaceable part without inheriting unrelated state. A sealed class is a class that cannot be used as a base class. A sealed override can prevent further overrides of that member while still allowing further class derivation. Neither sealed nor abstract is an automatic performance or security guarantee.
Reference and value semantics recap
Assigning a class reference copies the reference, so aliases reach the same object. Assigning an integer copies its value. This distinction matters for dependency objects shared by multiple consumers. Do not conclude that all reference types are mutable or that all value types live on the stack. Structs, records, boxing and detailed equality rules have their own later chapter.
using System;IFormatter plain = new PlainFormatter();IFormatter marked = new MarkedFormatter();Console.WriteLine(plain.Format("Box"));Console.WriteLine(marked.Format("Box"));interface IFormatter { string Format(string text); }sealed class PlainFormatter : IFormatter{ public string Format(string text) => text;}sealed class MarkedFormatter : IFormatter{ public string Format(string text) => "[" + text + "]";}The two implementations share a contract but do not require common base state. Sealed prevents subclassing; both classes still implement the interface.
Expected output (reasoned, not executed):
Box[Box]using System;int original = 4;int copy = original;copy = 9;Label first = new Label("Box");Label second = first;second.Text = "Shelf";Console.WriteLine(original);Console.WriteLine(first.Text);sealed class Label{ public Label(string text) => Text = text; public string Text { get; set; }}Changing the integer copy does not affect original. Assigning second copies the Label reference; changing its property affects the one shared object. The property is intentionally mutable for this demonstration.
Expected output (reasoned, not executed):
4ShelfCommon mistakes
An abstract class is not a class with no implementation. It can enforce common setup and contain concrete methods. Do not call overridable methods from a base constructor as a routine design: the derived object may not yet be fully initialized. Do not make a deep hierarchy solely to reuse a short string transformation.
Practice
Choose a design for two cases: (1) three report types require the same validated title and rendering sequence but each provides different body text; (2) two unrelated classes only need to expose Format(string). Implement a small example of one case and justify your choice.
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
Abstract classes and design trade-offs — knowledge check
Apply the lesson concepts to the following scenarios.
Can new Formatter(...) directly create an abstract Formatter?
Which member needs an implementation from a concrete derived class?
What does a sealed class prevent?
Two formatters share no state and only need Format. What is a suitable boundary?
After assigning a class reference to another variable, what changes if a shared mutable property is edited?