Skip to content
Search

Scala:Trait

lastmod 2020-08-26created 2020-08-261 edits

Traits are used to share interfaces and fields between classes.

They are similar to Java 8's interfaces. Classes and objects can extend traits, but traits cannot be instantiated and therefore have no parameters.

Defining a trait

A minimal trait is simply the keyword trait and an identifier:

trait HairColor

Traits become especially useful as generic types and with abstract methods.

trait Iterator[A] {
  def hasNext: Boolean
  def next(): A
}

See also

  • trait
author Zer0 sitemap