← Fundamentals
Fundamentals

Dispatch, inheritance, and composition

Which implementation runs for this call?

Trace which implementation a call selects and which object reused code receives.

Outcome

Say for a call whether the compiler or the running program picks the implementation, predict which version a shared or inherited method calls, spot a subclass that depends on how its base is written, and keep a wrapper’s own state in step with the value it forwards to.

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.

Go embedding promotes selectors for a contained field. It does not introduce subclass overriding: a promoted method retains the embedded value as receiver. Interface and constrained generic calls use the methods supplied by their concrete values.

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.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

type Claim interface{ Payout() int }

type Meal struct {
	Amount int
}

func (m Meal) Payout() int { return min(m.Amount, 30) }

func payStatic[T Claim](claim T) int { return claim.Payout() }

func payDynamic(claim Claim) int { return claim.Payout() }

func main() {
	meal := Meal{Amount: 45}

	fmt.Println(payStatic(meal), payDynamic(meal))
}
What is printed?
Go · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
go run main.go

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.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

type Claim struct{}

func (Claim) Limit() int { return 30 }

func (c Claim) Summary() string { return fmt.Sprintf("limit %d", c.Limit()) }

type HotelClaim struct {
	Claim
}

func (HotelClaim) Limit() int { return 120 }

func main() {
	fmt.Println(HotelClaim{}.Summary())
}
What is printed?
Loading A reused method follows its own receiver…

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.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

type Claim struct{}

func (Claim) Limit() int { return 30 }

func (c Claim) Summary() string { return fmt.Sprintf("limit %d", c.Limit()) }

type CappedClaim struct {
	inner Claim
}

func (CappedClaim) Limit() int { return 50 }

func (c CappedClaim) Summary() string { return c.inner.Summary() }

func main() {
	fmt.Println(CappedClaim{inner: Claim{}}.Summary())
}
What is printed?

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.

  1. easy · F-DISPATCH-V4-1 Bill the desks
  2. medium · F-DISPATCH-V4-2 Count the badges
  3. hard · F-DISPATCH-V4-3 Undo a scan
Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.