I have the below abstract class and its two subclasses:
abstract class BoundedNumber(val lowerBound: Double,
val upperBound: Double,
val value: Double) {
require(value >= lowerBound && value <= upperBound)
}
final case class Percentage(override val value: Double)
extends BoundedNumber(0, 100, value)
final case class Probability(override val value: Double)
extends BoundedNumber(0, 1, value)
Can I somehow implement a "generic" addition in BoundedNumber
even though it's abstract and cannot be instantiated?
abstract class BoundedNumber(val lowerBound: Double,
val upperBound: Double,
val value: Double) {
def +(that: BoundedNumber): BoundedNumber = {
require(this.getClass == that.getClass)
// This of course won't compile:
new BoundedNumber(lowerBound, upperBound, value + that.value)
}
}
Or am I bound to (pun intended) implement addition in both subclasses thus duplicating code?