Kotlin Generics and Variance (Compared to Java)

Kotlin Generics and Variance (Compared to Java)

Java, Kotlin, Software Development
This article covers the concepts of Generics and Variance in Kotlin and compares it to Java. Kotlin Generics differ from Java's in how users can define how they behave in sub-typing relationships. As opposed to Java, Kotlin allows defining variance on declaration-site, whereas Java only knows use-site variance. Kotlin Generics - What is Variance? Many programming languages support the concept of sub-typing, which allows implementing hierarchies that represent relationships like "A cat IS-an animal".Ā  In Java, we can either use the extends keyword to change/expand behavior of an existing class (inheritance) or use implements to provide implementations for an interface.Ā  According to Liskovā€™s substitution principle, every instance of a class A can be substituted by instances of its subtype B. The word variance, often referred to in mathematics as well,…
Read More