Ask about the constructed type, not only its element
Dog being a subtype of Animal does not automatically make a container of Dog a subtype of a container of Animal. Variance asks how a subtype relation propagates when types are used to construct larger types. Preserving the direction is covariance, reversing it is contravariance, and generally permitting neither direction is invariance. These names are easier to understand by inspecting reads and writes than by memorizing arrows. Our teaching relation treats Dog and Cat as Animal, while not every Animal is a Dog. It describes a simple interface model rather than every compatibility exception of a particular programming language. Look for an operation that would break before deciding which arrow is justified.
A read-only result can preserve the direction
Suppose ReadBox<Dog> promises to return Dog values and exposes no operation accepting replacements. A consumer requiring an Animal can use each returned Dog as an Animal. Under that interface, ReadBox<Dog> can occupy a ReadBox<Animal> use site: covariance in the element type. The reverse fails because an Animal-producing box might return Cat to a consumer requiring Dog. The argument depends on all exposed operations, not on the word Read in a class name. Hidden implementation mutability and operations available through the interface are separate questions. Adding a method accepting T can change the reasoning, so an interface that used to justify covariance must be reconsidered when its operation set expands.
Writing creates a counterexample
Assume a mutable Box<Dog> could be passed as Box<Animal>. The recipient may then insert Cat because its interface promises that any Animal is acceptable. The original Dog consumer later retrieves Cat and attempts a Dog-only operation such as bark. This counterexample defeats covariance for the simple read/write box. Reversing the direction is not generally safe either: Box<Animal> might already contain Cat when treated as Box<Dog>. Our model therefore makes this parameter invariant. Real languages may instead use runtime checks for certain assignments or expose a separate read-only view. Identify that mechanism before extending this example’s conclusion to every array or generic container in every language.
Function inputs depend on what can be accepted
If a caller needs a function able to process Dog, a function able to process every Animal can satisfy that need: Dog is among its accepted inputs. A Dog-only function cannot replace a function that must handle arbitrary Animal because the caller may supply Cat. This gives the reverse, contravariant direction for function inputs in our model. Results follow the other direction: a function returning Dog can satisfy a consumer requiring an Animal result, giving covariance in the return type. When one type variable appears in both inputs and outputs, examine both requirements together. Do not summarize the rule as all function types reverse. Track what the caller may provide and what the receiver promises to return.
Use counterexamples before memorizing syntax
Languages differ in declaration-site variance, use-site bounds and structurally derived compatibility rules. A symbol learned in one language cannot automatically be copied into another with the same meaning. Separate reading the declaration from explaining why the permitted direction preserves the operations. Escape hatches, special types and dynamically checked values can alter the scope of static guarantees as well. This lesson derives direction from reads and writes rather than cataloging compiler-specific compatibility exceptions. When applying it to source, identify the language and version and test both an allowed assignment and a rejected one. Successful compilation is an observation about those rules, not a replacement for explaining the intended safety argument.
Exercise: ask whether Cat can be inserted
Explain the ReadBox<Dog> to ReadBox<Animal> direction: a read returns a Dog usable as Animal, under the read-only premise. Apply the same proposed direction to a mutable Box and ask whether Cat may be inserted. The Box<Animal> consumer is allowed to insert Cat, breaking the original Dog-only reading expectation. Finally replace a Dog-processing callback with an Animal-processing function; that function can accept Dog, while the reverse substitution has Cat as a counterexample. In every answer, identify who produces the value, who receives it and which operations are available. Preserve that explanation when designing real generic interfaces or callbacks. Changes to the operation set require reconsidering the previously justified direction.