October 2025
Intermediate to advanced
100 pages
2h 27m
English
A Strawberry is a Fruit, so it’s easy to assume that an array of strawberries should always count as an array of fruits. But that’s only half the story. The Array<Fruit> type doesn’t just specify that every object you retrieve from the array will be a Fruit. It also gives you permission to place any Fruit object into the array.
If Kotlin allowed us to treat the same array as both an Array<Strawberry> and an Array<Fruit>, we’d have one piece of code believing every item in the array to be a strawberry, while another part of the code would have free rein to replace those strawberries with different fruits. That’s a recipe for errors at runtime, so Kotlin won’t let us treat these two array types as interchangeable.
Read now
Unlock full access