July 2017
Intermediate to advanced
796 pages
18h 55m
English
In order to extend traits or classes, you need to use the extend keyword. Traits cannot be instantiated because it may contain unimplemented methods. So, it's necessary to implement the abstract members in the trait:
trait Cat extends Animal{ }
A value class is not allowed to extend traits. To permit value classes to extend traits, universal traits are introduced, which extends for Any. For example, suppose that we have the following trait defined:
trait EqualityChecking { def isEqual(x: Any): Boolean def isNotEqual(x: Any): Boolean = !isEqual(x)}
Now, to extend the preceding trait in Scala using the universal trait, we follow the following code segment:
trait EqualityPrinter extends Any { def print(): Unit = println(this ...
Read now
Unlock full access