December 2015
Intermediate to advanced
298 pages
5h 30m
English
As we know well by now, programming in Scala is super simple. Actually, this is one major use of traits, namely to modify the existing class methods of an object. I am just giving the new methods now. You can refer to the following sample code for a working example:
abstract class Animal(val rating: Int) { def giveInoculation(): Unit // abstract method def alreadyInoculated() : Boolean // abstract method } trait FilterOutAlreadyInoculated extends Animal { abstract override def giveInoculation(): Unit = // 1 if (!alreadyInoculated()) super.giveInoculation() } class Horse(rating: Int, var inoculated: Boolean = false) extends Animal(rating) with Walks with Ordered[Animal] {
Read now
Unlock full access