Terminology (6)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Dynamic dispatch
- Choosing the implementation from the value while the program runs: an override, a virtual function, an interface or trait object.
- Static dispatch
- Choosing the implementation at compile time from the declared or generic type.
- Override
- A subclass method that replaces the base version for calls dispatched on the object.
- Embedding (Go)
- Including a type as an unnamed field, which promotes its methods; they still run on the inner value.
- Composition
- Reusing code by holding another value and forwarding calls to it.
- Fragile base class
- A subclass that breaks when the base changes how its own methods call each other.
Selecting and reusing behavior
A receiver identifies the value a method uses; dispatch selects the implementation for a call. Static selection uses compile-time type information, while dynamic dispatch can select behavior from the runtime value. The call form matters as well as the method name.
Inheritance reuses behavior through a base relationship. Composition holds a collaborator and delegates selected work to it. In either design, follow internal calls as well as the entry call: reused code may reach a helper on its own receiver or forward to a different value.
TypeScript class inheritance follows JavaScript object behavior. Ordinary member calls use the runtime object’s properties, even when a variable has a base-type annotation. Holding another object instead makes delegation an explicit call on that object.
01Who chooses
The compiler, or the running program
A coworking subscription desk quotes monthly workspace plans and logs the visitor badges issued at check-in. Standing workspaces add a configured 50 surcharge to the 200 base price. The badge log records single scans and whole batches as transactions; undo removes the last transaction, so its count must follow the entries that actually remain.
Before repairing plan quotes, use a small claim calculator to isolate dispatch from rental arithmetic. The meal claim holds 45 and caps payout at 30; follow each general-type or generic call and predict which implementation runs.
class Claim {
amount: number;
constructor(amount: number) {
this.amount = amount;
}
payout(): number {
return this.amount;
}
}
class MealClaim extends Claim {
payout(): number {
return Math.min(this.amount, 30);
}
}
const claim: Claim = new MealClaim(45);
console.log(claim.payout());
npx tsc --noEmit --strict --target ES2022 main.ts && node main.ts
Use the setup from the first reading for your language.
02Inherited methods
Which version a shared method calls
A reused bill can call another method internally. This claim-summary probe isolates that second hop: shared summary calls limit, while the hotel claim supplies 120. Predict which helper implementation the shared body reaches.
class Claim {
limit(): number {
return 30;
}
summary(): string {
return `limit ${this.limit()}`;
}
}
class HotelClaim extends Claim {
limit(): number {
return 120;
}
}
console.log(new HotelClaim().summary());
03Composition
Forwarding runs the held value’s own methods
Badge counters will also forward work to a held log. First use a simpler claim wrapper: it has its own limit of 50 but delegates summary to a held basic claim. Predict what that specific forwarding path reports.
class Claim {
limit(): number {
return 30;
}
summary(): string {
return `limit ${this.limit()}`;
}
}
class CappedClaim {
inner: Claim;
constructor(inner: Claim) {
this.inner = inner;
}
limit(): number {
return 50;
}
summary(): string {
return this.inner.summary();
}
}
console.log(new CappedClaim(new Claim()).summary());
04Repair the subscription desk
Quote the selected plan, reconcile badge batches, then undo a transaction
First quote each selected desk plan for its rental months using that plan’s monthly price. Then reconcile the visitor badge count with both single and batch scans. Finally undo the last recorded transaction: a batch undo removes every badge in that batch, while a single undo removes one.
The count summarizes the badges retained in the log. The undo version supplies a transaction-grouped Log, so inspect its actual method bodies again when moving from batch counting to history. The supplied single/batch/undo witnesses exercise calls that dispatch back into a specialized method and calls forwarded to another object. Each native has its own mechanism; inheritance syntax alone does not establish the contract.
Repair the subscription desk
Which version runs, a base that calls back, then a wrapper that keeps its own state.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?