Scala - Trait (Interface)

Card Puncher Data Processing

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].





Discover More
Card Puncher Data Processing
Scala - Main (Top-Level Objects)

In Scala, the main or entry point method is defined in an object. An object can be made executable by either: adding extending the type App or by adding a method object extends the type...
Card Puncher Data Processing
Scala - Mixin (Composition)

When a trait is being mixed into a class or trait, it is called a mixin.
Card Puncher Data Processing
Scala - Type

Every variable and expression in a Scala program has a type that is known at compile time. A type restricts the possible values to which a variable can refer, or an expression can produce, at run time....



Share this page:
Follow us:
Task Runner