E037: Overrides Nothing — 오버라이드할 대상이 없어요

E037: Overrides Nothing — 오버라이드할 대상이 없어요

override 수식어를 붙여 선언한 멤버가 있는데, 슈퍼클래스에 그에 대응하는 멤버가 없을 때 이 에러가 나와요.

출처: Scala 3 Reference

본문

오버라이드하려면 슈퍼클래스에 같은 이름을 가진 필드나 메서드가 있어야 해요. 이 에러는 주로 이런 상황에서 발생해요.

  • 멤버 이름을 잘못 적었을 때
  • 잘못된 클래스를 상속하고 있을 때
  • 부모 클래스에 그 이름의 멤버가 없을 때

예제 (Example)

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

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

Parent에는 greet가 있는데, Childgreeet로 오타가 났어요. 그래서 오버라이드할 대상이 없죠.

오류 메시지 (Error)

-- [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)

부모 메서드와 철자를 맞추거나, 오버라이드가 아니라면 override 수식어를 제거하면 돼요.

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

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

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

더 알아보기 (Learn more)