Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override final method

Final methods can not be overridden in a subclass. But with the magic of Scala it seems as this is possible.

Consider the following example:

trait Test {
  final def doIt(s: String): String = s
}

object TestObject extends Test {
  def doIt: String => String = s => s.reverse
}

The method doIt in the object TestObject has not the same signature as doIt in trait Test. Thus doIt is overloaded instead of overridden. But a normal call to doIt always calls the method in TestObject:

val x = TestObject.doIt("Hello")                //> x  : String = olleH

Question: How can I call the original method doIt on the TestObject. Is this possible or is this method "sort of overridden"?

like image 273
Erik Avatar asked Dec 08 '13 12:12

Erik


1 Answers

You can use TestObject as Test like this:

(TestObject: Test).doIt
like image 197
senia Avatar answered Oct 01 '22 08:10

senia