A class can only extend from one class or trait, any subsequent extension should use the keyword with
:
Traits are polymorphic.
Traits are polymorphic. Any type can be referred to by another type if related by extension:
Similar to interfaces in Java, traits are used to define object types by specifying the signature of the supported methods.
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, traits may not have constructor parameters.
Here is an example:
trait Similarity { def isSimilar(x: Any): Boolean def isNotSimilar(x: Any): Boolean = !isSimilar(x) }
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 for isSimilar
. The behavior for isNotSimilar
gets inherited directly from the trait.
A class uses the extends
keyword to mixin a trait if it is the only relationship the class inherits:
The parameterless execute method has been deprecated and will be removed in a future version of ScalaTest. Please invoke execute with empty parens instead: execute().
The trap method is no longer needed for demos in the REPL, which now abreviates stack traces, and will be removed in a future version of ScalaTest