E038: Overrides Nothing But Name Exists
This error is emitted when a member is declared with the override modifier and a member with the same name exists in the parent class, but the signatures don't match.
There must be a non-final field or method with the same name and the same parameter list in a super class to override it. This error indicates that while the name matches, either the parameter types or the return type differ.
Example
class Parent:
def process(x: Int): String = x.toString
class Child extends Parent:
override def process(x: String): String = x
Error
-- [E038] Declaration Error: example.scala:5:15 --------------------------------
5 | override def process(x: String): String = x
| ^
| method process has a different signature than the overridden declaration
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| There must be a non-final field or method with the name process and the
| same parameter list in a super class of class Child to override it.
|
| def process(x: String): String
|
| The super classes of class Child contain the following members
| named process:
| def process(x: Int): String
-----------------------------------------------------------------------------
Solution
// Match the parameter types of the parent method
class Parent:
def process(x: Int): String = x.toString
class Child extends Parent:
override def process(x: Int): String = s"Child: $x"
// Or use overloading instead of overriding
class Parent:
def process(x: Int): String = x.toString
class Child extends Parent:
def process(x: String): String = x
In this article