Home:ALL Converter>What is the purpose of overriding a trait function with a class value in Scala?

What is the purpose of overriding a trait function with a class value in Scala?

Ask Time:2015-07-29T12:08:00         Author:Luong Ba Linh

Json Formatter

I am new to Scala. I just wonder why we sometimes, especially in Scala API, override trait function with a class value like the example below:

sealed trait Amount {
  def amount: Double
}

case class Percentage(amount: Double) extends Amount {
  override def toString = s"$amount%"
}

Author:Luong Ba Linh,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31691073/what-is-the-purpose-of-overriding-a-trait-function-with-a-class-value-in-scala
yy