This is the polymorphism every developer knows about, and it's related to overriding methods in concrete class implementations. Consider the following simple hierarchy:
abstract class Item { def pack: String}class Fruit extends Item { override def pack: String = "I'm a fruit and I'm packed in a bag."}class Drink extends Item { override def pack: String = "I'm a drink and I'm packed in a bottle."}
Now, let's have a shopping basket of items and call pack for each of them:
object SubtypePolymorphismExample { def main(args: Array[String]): Unit = { val shoppingBasket: List[Item] = List( new Fruit, new Drink ) shoppingBasket.foreach(i => System.out.println(i.pack)) }}
As you can see, here we can use the abstract type and just ...