Back to Scala3

E037: Overrides Nothing

docs/_docs/reference/error-codes/E037.md

3.8.41.9 KB
Original Source

E037: Overrides Nothing

This error is emitted when a member is declared with the override modifier but there is no corresponding member in a superclass to override.

There must be a field or method with the same name in a super class to override it. This error typically occurs when:

  • The member name is misspelled
  • The wrong class is being extended
  • The parent class doesn't have a member with that name

Example

scala
class Parent:
  def greet(): String = "Hello"

class Child extends Parent:
  override def greeet(): String = "Hi"

Error

scala
-- [E037] Declaration Error: example.scala:5:15 --------------------------------
5 |  override def greeet(): String = "Hi"
  |               ^
  |               method greeet overrides nothing
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | There must be a field or method with the name greeet in a super
  | class of class Child to override it. Did you misspell it?
  | Are you extending the right classes?
   -----------------------------------------------------------------------------

Solution

scala
// Fix the spelling to match the parent method
class Parent:
  def greet(): String = "Hello"

class Child extends Parent:
  override def greet(): String = "Hi"
scala
// Or remove the override modifier if not overriding
class Parent:
  def greet(): String = "Hello"

class Child extends Parent:
  def greeet(): String = "Hi"
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>