August 2018
Intermediate to advanced
380 pages
10h 2m
English
The concept of an object-oriented interface is encapsulated in a trait in Scala. Similarly to an interface, a trait can have abstract members. However, unlike Java interfaces, traits may also have concrete members. These are injected into the implementing classes:
scala> :paste// Entering paste mode (ctrl-D to finish)trait Foo { def saySomething = println("I am inherited from Foo")}// Exiting paste mode, now interpreting.defined trait Foo
Just like in Java, Scala classes can implement more than one trait. However, since traits in Scala can have concrete members, a new inheritance model that allows for that is required.
In Scala, a so-called linearization model is implemented. This means that whenever a class is inherited from ...