Scala Traits in action
Scala Traits are somewhat similar to Java 8's interface, but they do more. We can use a Scala Trait as an interface (contract), abstract class, class, Mixin, and more.
In Scala, a Trait can contain abstract code, concrete code, or both. We can create a trait using the trait
keyword, as shown here:
Trait Syntax: trait <Trait-Name> { // Abstract members (Data and Functions) // Non-Abstract (Concrete) members (Data and Functions) }
Let's explore them, one by one, now.
Trait as an interface
We can define a Trait only with abstract members, as shown here:
rambabuposa@ram$ scala -cp joda-time-2.9.6.jarWelcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_121). Type in expressions for evaluation. Or try :help. scala> case class Weather(day: String, temparature: String) defined class Weather scala> :paste // Entering paste mode (ctrl-D to finish) import org.joda.time.LocalDate trait IWeatherForecasting { def getWeatherForecasting...