E038: Overrides Nothing But Name Exists — 이름은 있는데 오버라이드가 안 돼요

E038: Overrides Nothing But Name Exists — 이름은 있는데 오버라이드가 안 돼요

override 수식어를 붙인 멤버가 있고 부모 클래스에도 같은 이름의 멤버가 존재하지만, 시그니처(서명)가 일치하지 않을 때 이 에러가 나와요.

출처: Scala 3 Reference

본문

오버라이드하려면 슈퍼클래스에 같은 이름 그리고 같은 파라미터 목록을 가진 non-final 필드나 메서드가 있어야 해요. 이 에러는 이름은 맞는데, 파라미터 타입이나 반환 타입이 다를 때 발생해요.

예제 (Example)

class Parent:
  def process(x: Int): String = x.toString

class Child extends Parent:
  override def process(x: String): String = x

Parent.processInt를 받는데 Child.processString을 받아요. 이름은 같지만 시그니처가 달라서 오버라이드가 되지 않죠.

오류 메시지 (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)

부모 메서드의 파라미터 타입을 맞추거나, 오버라이드 대신 오버로딩(overloading)을 사용하면 돼요.

// 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

더 알아보기 (Learn more)