# Covariance

DevFeed: [Covariance](<https://devfeed.tech/articles/covariance-25011.md>)

Original publisher: [Read original article](<https://typealias.com/concepts/covariance/>)

Author: author@typealias.com (Dave Leeds)

Published: 2017-11-14T00:00:00Z

Content type: article

Language: en

Sources: [Dave Leeds on Kotlin - typealias.com](<https://devfeed.tech/sources/dave-leeds-on-kotlin-typealias-com.md>)

Topics: [data type](<https://devfeed.tech/topics/data-type.md>), [generic programming](<https://devfeed.tech/topics/generic-programming.md>), [Object-oriented programming (OOP)](<https://devfeed.tech/topics/oop.md>), [Inheritance](<https://devfeed.tech/topics/inheritance.md>), [subclass](<https://devfeed.tech/topics/subclass.md>), [superclass](<https://devfeed.tech/topics/superclass.md>), [class](<https://devfeed.tech/topics/class.md>)

Tags: [contravariance](<https://devfeed.tech/tags/contravariance.md>), [covariance](<https://devfeed.tech/tags/covariance.md>), [generic](<https://devfeed.tech/tags/generic.md>), [generics](<https://devfeed.tech/tags/generics.md>), [inheritance](<https://devfeed.tech/tags/inheritance.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [programming](<https://devfeed.tech/tags/programming.md>), [subtype](<https://devfeed.tech/tags/subtype.md>), [type-parameter](<https://devfeed.tech/tags/type-parameter.md>), [variance](<https://devfeed.tech/tags/variance.md>)

## AI overview

Covariance means two type relationships subtype in the same direction. In Kotlin, a read-only List is covariant: if B is a subtype of A, List<B> can be used where List<A> is expected. MutableList is not covariant, since allowing that substitution could make writes unsafe. Generic classes are invariant by default; marking a parameter `out` permits covariance but requires the class to only produce that type, not accept it as a function argument or setter value. The excerpt also notes that subclass overrides may use a more specific return type, though its example is cut off.

## Source excerpt

On the surface, subtypes seem straightforward. Most programmers who write object-oriented code are familiar with the concept of inheritance, and how you can pass off a subclass as if it were its superclass. But subtyping rules aren't always as intuitive when you start working with generics. What is Covariance? Covariance describes a relationship between two sets of types where they both subtype in the same direction. For example, for our first set of types, here are two classes, A and B, where B is a subtype of A: