Non-Variance

Non-Variance is the most basic and easiest variance on parameterized types. According to non-variance or we can say in-variance there is no relationship between any of two parameterize type (as shown in diagram 10.1), even their type constructor accepts those parameters which have some inheritance hierarchy like our previous example Garage[Lamborghini] is completely different type from Garage[Car] even Car is a supertype of Lamborghini. By default when we creating our parameterized types they are non-variant.

class Basket[T](fruitsOrVeg : T) {
	def addTheFuritOrVeg(fruitsOrVeg : T) : Unit = { … }
}

class Fruit
class Apple extends Fruit
class Orange extends Fruit

new Basket[Fruit]
new Basket[Apple]
new Basket[Orange]

We are creating three new parameterize type objects which are completely different from each other even Fruit have an inheritance hierarchy.

Last updated