Traits and Lists
Learn about traits used in place of interfaces and lists in Scala.
We'll cover the following...
Traits as Mixins
Scala does not have interfaces. But it has Traits instead, which is somewhat like a combined Interface and Abstract-base-class in Java. However, unlike anything in Java, Traits can be applied to classes or instances of classes and multiple Traits can be used.
Here’s an example using a Trait:
Press + to interact
class SpaceShuttle(val name:String) {def launch = println(name + " Take off!")}trait MoonLander {def land = println("Landing!")}class MoonShuttle(name:String) extends SpaceShuttle(name) with MoonLander {}val apollo = new MoonShuttle("Apollo 27")apollo.launchapollo.land
A class can “extend” any ...