Frequently, we can skip the type declaration on our variable or property. Kotlin will infer the generic type based on the value we use to initialize the variable or property. Here, we are using mutableListOf() to create a MutableList of objects — a MutableList allows us to change its contents after initialization.

If critters in this sample were initialized as:

val critters = mutableListOf(Frog(), Axolotl())

...then Kotlin would see that both of these objects share Animal as a common base type, so critters would be treated as a MutableList<Animal>.

However, the list we are using in this snippet also has a String when we initialize it. The common base type for Frog, Axoltl, and String is Any, the overall base class for all Kotlin classes. So, Kotlin will treat critters as a MutableList<Any>.

You can learn more about this in:
Run Edit