Tutorial by Examples

You can use traits to modify methods of a class, using traits in stackable fashion. The following example shows how traits can be stacked. The ordering of the traits are important. Using different order of traits, different behavior is achieved. class Ball { def roll(ball : String) = println(&q...
This is the most basic version of a trait in Scala. trait Identifiable { def getIdentifier: String def printIndentification(): Unit = println(getIdentifier) } case class Puppy(id: String, name: String) extends Identifiable { def getIdentifier: String = s"$name has id $id" } ...
The diamond problem, or multiple inheritance, is handled by Scala using Traits, which are similar to Java interfaces. Traits are more flexible than interfaces and can include implemented methods. This makes traits similar to mixins in other languages. Scala does not support inheritance from multipl...
In case of stackable modification, Scala arranges classes and traits in a linear order to determine method call hierarchy, which is known as linearization. The linearization rule is used only for those methods that involve method invocation via super(). Let's consider this by an example: class Shap...

Page 1 of 1