scala> // Traits can be mixed-in with other Classes #257
scala> // Advantage over Inheritance
scala> // - Multiple Traits can be mixed-in #257
scala> // - super.xxx() calls are dynamically bound rather than
scala> // statically bound (Can be used to implement Stackable
scala> // modifications #260)
scala> // Limitation
scala> // - Traits cannot have Class Parameter #260
scala>
scala> //////////////////////////////////////////////
scala> // Create a Trait
scala> //////////////////////////////////////////////
scala> //Note here : math implicitly extends 'AnyRef'
scala> trait math {
| def score() = {
| println("Score is 80")
| }
| }
defined trait math
scala>
scala> trait science {
| def mark() = {
| println("Score is 90")
| }
| }
defined trait science
scala>
scala> class history
defined class history
scala>
scala> //////////////////////////////////////////////
scala> // Example 1 : Understanding the Syntax
scala> //////////////////////////////////////////////
scala> class Subject extends history with math with science
defined class Subject
scala> val sub = new Subject
sub: Subject = Subject@2df9c215
scala> sub.score()
Score is 80
scala>
scala> //////////////////////////////////////////////
scala> // Example 2 : Overriding a method
scala> //////////////////////////////////////////////
scala> class Subject extends history with math with science {
| override def score() {
| println("New score is 75")
| }
| }
defined class Subject
scala> val sub = new Subject
sub: Subject = Subject@19304519
scala> sub.score()
New score is 75
scala>
scala> //////////////////////////////////////////////
scala> // Example 3 : Traits cannot have Class
scala> // Parameter
scala> //////////////////////////////////////////////
scala> trait AnotherTrait(no: Int)
<console>:1: error: traits or objects may not have parameters
trait AnotherTrait(no: Int)
^
PS : A trait mixed into a Class can be called as
Mixins #269