Skip to content

Instantly share code, notes, and snippets.

@amita-shukla
Last active March 11, 2021 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amita-shukla/32f14cf325aa89296b10297862fc198f to your computer and use it in GitHub Desktop.
Save amita-shukla/32f14cf325aa89296b10297862fc198f to your computer and use it in GitHub Desktop.
class Animal(val name: String){
def speak = "animal speaking"
def eat = "animal eats"
override def toString = "animal: " + name
}
class Cat(override val name: String) extends Animal(name){
override def speak = "meow"
override def eat = "fish"
def catonly : String = "cat only"
}
class Dog(override val name: String) extends Animal(name){
override def speak = "woof"
override def eat = "bone"
def dogonly : String = "dog only"
}
val animal1 = new Animal("animal1")
val animal2 = new Animal("animal2")
val cat1 = new Cat("cat1")
val cat2 = new Cat("cat2")
val dog1 = new Dog("dog1")
val dog2 = new Dog("dog2")
/* Subtyping */
val animals : List[Animal] = List(animal1, animal2)
val allTypesOfAnimals : List[Animal] = cat1 :: dog1 :: animals // possible: can use a subtype where supertype is expected
val cats : List[Cat] = List(cat1, cat2)
//val brokenCats : List[Cat] = animal1 :: cats // type mismatch: can't use supertype where subtype is expected
/* CoVariance: Lists */
// case1:
def expectingListOfSupertype(animals : List[Animal]): Unit ={
println(animals.map(_.toString))
}
expectingListOfSupertype(cats) // possible: can pass a list of subtypes where list of supertype is expected
// case2:
def expectingListOfSubtype(cats: List[Cat]): Unit = {
println(cats.map(_.catonly))
}
//expectingListOfSubtype(animals) //type mismatch: can't pass a list of supertype where list of subtype is expected
/* ContraVariance: Functions */
// case1:
val superTypeFunction : Animal => String = animal => s"${animal.name} is here"
def functionThatExpectsSupertypeFunction(fun: Animal => String) = {
val fish = new Animal("nemo")
println(fun(fish))
}
//functionThatExpectsSupertypeFunction(subTypeFunction) // type mismatch: can't pass a function of subtype where a function of supertype is expected
// case 2:
val subTypeFunction : Cat => String = cat => s"${cat.name} cat is here"
def functionThatExpectsSubtypeFunction(fun: Cat => String) = {
val cat = new Cat("kitty")
println(fun(cat))
}
functionThatExpectsSubtypeFunction(superTypeFunction) // possible: can pass a function of supertype where a function of subtype is expected
/* InVariance : Arrays */
val animalarray: Array[Animal] = Array(animal1, animal2)
val catarray: Array[Cat] = Array(cat1)
def expectingArrayOfSupertype(animals: Array[Animal])= {
animals(0) = cat1 // write
animals(1) // read
}
def expectingArrayOfSubtype(cats: Array[Cat]) = {
cats(0) = cat2 // write
cats(1) // read
}
// case 1:
// expectingArrayOfSupertype(catarray) // type mismatch: can't pass an array of subtype where an array of supertype is expected
// case 2:
// expectingArrayOfSubtype(animalarray) // type mismatch: can't pass an array of supertype where an array of subtype is expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment