Scala allows developers to extend many traits in a single class. This adds the possibility of achieving multiple inheritance and saves a lot of effort in code writing, which has to be performed in languages where extending many classes is not allowed. In this subtopic, we will show how traits can be mixed in a specific class or used to create anonymous classes with some specific functionality while writing our code.
Mixin compositions
Mixing traits in
First of all, let's modify the code from the previous example. It is a really simple change and it will also show exactly how traits can be mixed in:
object MixinRunner extends Ping with Pong {
def main(args: Array[String]): Unit = {
ping()
pong()
}
}
As can be seen...