```class VeganFood: Food() interface Seller<ou...
# getting-started
m
Copy code
class VeganFood: Food()

interface Seller<out T>

class FoodSeller: Seller<Food>
class VeganFoodSeller: Seller<VeganFood>

interface Consumer<in T>

class Person: Consumer<Food>
class Vegan: Consumer<VeganFood>

fun main() {
    var foodSeller: Seller<Food>
    foodSeller = FoodSeller()
    foodSeller = VeganFoodSeller()
    var veganFoodConsumer: Consumer<VeganFood>
    veganFoodConsumer = Vegan()
    veganFoodConsumer = Person()
}
Does anyone explain covariant and contravariant in Kotlin, please recommend any discussion on this topic
k
I think the Kotlin documentation explains it quite well https://kotlinlang.org/docs/generics.html
Which part was unclear?
m
<in T>
means the T can only go IN the class e.g.:
fun add(thing: T): Boolean
<out T>
means the T can only go OUT the class e.g.:
fun get(index: Int): T
most things are both in and out, and thus just
T
but a consumer, like the name suggest, would probably only need to have things go IN it. same goes for a producer, but for OUT. is that clear enough?
m