Concepts and theorems · Intermediate

Subtyping: substitution under an expected interface

Reason from the operations a consumer expects, separating subtyping from code inheritance, structural compatibility, mutation and behavioral requirements.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Start from the consumer’s requirements
  2. Separate inheritance from the subtype relation
  3. Compare read-only structures first
  4. Distinguish type compatibility from behavioral promises
  5. Search for a counterexample to the reverse direction
  6. Exercise: enumerate the operations used
  7. Ask Logeia about this articleCheck your understanding

Start from the consumer’s requirements

Subtyping expresses when a value of one type may be used where another type is expected. Consider a teaching Animal interface whose name can be read, and a Dog interface that also provides bark. A consumer using only Animal’s readable name can be satisfied by Dog. Write that direction as Dog being a subtype of Animal. The reverse does not follow: not every Animal supplies bark. Similar names or a class-inheritance drawing are not enough to determine substitutability. Ask what the consumer may assume under the chosen language’s type rules. This lesson begins with deliberately read-only interfaces so that the required operations are explicit rather than concealed inside a larger implementation.

Separate inheritance from the subtype relation

Inheritance is a mechanism for reusing or extending definitions and implementation. Subtyping concerns which values are admissible at a use site. Some languages connect class inheritance to subtyping, but that relationship is not identical across languages. A nominal system uses declared identities or relationships; a structural example can accept any record meeting the required readable fields without a common parent-class name. Matching field spelling alone is still insufficient. A name field containing an integer does not satisfy a consumer expecting a string merely because both records contain a field called name. Separate implementation reuse from the interface contract offered to a consumer, and identify the language rules behind a particular compatibility decision.

Compare read-only structures first

Suppose a consumer reads only a string-valued name, while a supplied value contains both name and age. Under the stated read-only structural rule, the extra age information need not obstruct use because the required field remains available with the right type. Change the contract to permit writes and the reasoning changes. A consumer’s assignment could violate an expectation held by the original provider of that value. Compare supported operations as well as field lists. A direction valid for reading cannot automatically be transferred to a mutable box or array. The variance lesson makes that failure concrete by contrasting a container that only returns values with one that also accepts arbitrary replacements.

Distinguish type compatibility from behavioral promises

Matching types do not establish every application-level expectation. Two withdraw operations may accept and return the same types while one performs an unexpected transfer to another account. That behavior is not justified merely by a compatible signature. Preconditions, results and state changes may also matter to a consumer’s contract. Do not present a subtype rule as automatically proving every property of a program. Similarly, an operation intended only for an authorized actor does not become available to everyone whose argument has the correct type. Static compatibility, runtime behavior and service permissions are separate responsibilities. Keeping them distinct explains why testing, validation and authorization can remain necessary after type checking succeeds.

Search for a counterexample to the reverse direction

A consumer requiring Dog may call bark. Passing an arbitrary Animal fails if even one admissible Animal lacks that operation. One counterexample is enough to reject an unconditional compatibility rule in that direction. When learning a type relation, select a required operation and search for a value that cannot provide it. Extra operations are not automatically a problem either: under this read-only example, the consumer’s permitted assumptions remain the central issue. Real languages add access rules, generics, mutability and special types that affect assignment decisions. A teaching arrow diagram is not evidence that the same source assignment compiles in every language. Connect a language specification or compiler observation to the exact conditions represented by the example.

Exercise: enumerate the operations used

Explain why a function reading only name can accept the teaching Dog interface: Dog supplies the required readable name. Then examine a function calling bark with an Animal value containing only name; the missing operation is a counterexample. Finally consider a consumer reading both name and age with a record supplying only name. Our structural rule rejects it because age is missing. In each case, write the consumer’s permitted operations and their types instead of relying on an impression from the type names. Add an example where types match but application behavior violates a separate promise, and identify that extra contract. Separating assignment compatibility from behavioral guarantees prepares the ground for variance and API design.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 19

Choose a learning path