A Tour of Scala: Traits

Similar to interfaces in Java, traits are used to define object types by specifying the signature of the supported methods. Unlike Java, Scala allows traits to be partially implemented; i.e. it is possible to define default implementations for some methods. In contrast to classes[1], traits may not have constructor parameters.

This trait consists of two methods isSimilar and isNotSimilar. While isSimilar does not provide a concrete method implementation (it is abstract in the terminology of Java), method isNotSimilar defines a concrete implementation. Consequently, classes that integrate this trait only have to provide a concrete implementation forisSimilar. The behavior for isNotSimilar gets inherited directly from the trait. Traits are typically integrated into a class[2] (or other traits) with a mixin class composition[3]: