Scala - Trait (Interface)

About

Traits are like interfaces in Java, but they can also contain concrete members, i.e. method implementations or field definitions.

A trait, which is defined with the trait keyword;

  • is like an abstract class that cannot take any value parameters
  • can be “mixed into” classes or other traits via the process known as mixin composition. When a trait is being mixed into a class or trait, it is called a mixin.

A trait may be parameterized with one or more types.

When parameterized with types, the trait constructs a type. For example,

  • Set is a trait that takes a single type parameter, whereas
  • Set[Int] is a type.

Also, Set is said to be “the trait of” type Set[Int].


Powered by ComboStrap