O'Reilly logo

Scala Design Patterns by Ivan Nikolov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Polymorphism

Polymorphism is something every developer who has done some object-oriented programming knows about.

Note

Polymorphism helps us to write generic code that can be reused and applied to a variety of types.

It is important to know that there are different types of polymorphism out there, and we will be looking at them in this section.

Subtype polymorphism

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required