I would like subclasses to inject functionality within the base class' apply method:
Base class:
case class BaseClass(data: Json)
object BaseClass {
def apply(data: String) : JsonClass = {
try {
_ //subclass functionality should go here
} catch {
case e: Exception => ErrorJsonClass(data)
}
}
}
object SubClass extends BaseClass {
def apply(data: String) : Json = {
deserialize(data) // this should be called within the base class' apply
}
}
Is there a way to do this without declaring separate methods within the base class?
Ie. Im trying to avoid the following:
object BaseClass {
def apply(data: String) : JsonClass = {
try {
convert(data)
} catch {
case e: Exception => ErrorJsonClass(data)
}
}
def convert(data: String): JsonClass = _
}
object SubClass extends BaseClass {
def convert(data: String) : Json = {
deserialize(data)
}
}