Why must a method either have a body or be marked abstract?

← Classes and OOP · Ref: Q773

EK9 requires explicit intent for every method. A method with no body and no 'as abstract' keyword is ambiguous — did you forget to implement it, or did you intend it to be abstract? EK9 does not guess.

THE RULE

- Method WITH body: concrete implementation (normal method)
- Method WITHOUT body + 'as abstract': abstract, subclass must implement
- Method WITHOUT body + NO abstract: ERROR E07110

THIS EXAMPLE

The Validator class has a concrete validate() method with a body. The Formatter function has a concrete body. Both are unambiguous.

COMMON CAUSES

1. Forgot to add implementation body
2. Forgot the 'as abstract' keyword
3. Incomplete method definition during development

THIS PAIRS WITH E07100

E07100 is the opposite: abstract WITH a body. Together they enforce that abstract and body are mutually exclusive — you must choose one.

See Q768 for abstract methods (opposite rule). See Q86 for abstract classes. See Q93 for defining classes.

Example

defines module qa.classesandoop.methodbody

  defines function

    Formatter() as pure
      -> input as String
      <- output as String: input.trim()

  defines class

    Validator
      minLength <- Integer()

      Validator()
        -> min as Integer
        minLength :=: min

      validate()
        -> input as String
        <- rtn as Boolean: length input > 0

      default operator ?

  defines program

    ShowValidation()
      stdout <- Stdout()
      checker <- Validator(3)
      if checker?
        stdout.println($checker.validate("hello"))
        stdout.println(Formatter("  padded  "))

Common mistakes

E07110 — Removing the initialisation from the return variable leaves the method with no body. The method is not marked 'as abstract', so EK9 cannot determine intent. Either add an implementation or mark the method 'as abstract' (and make the class abstract). See ek9 -h E07110 for details.

Incorrect:

      validate()
        -> input as String
        <- rtn as Boolean?

Correct:

      validate()
        -> input as String
        <- rtn as Boolean: length input > 0

E07110 — The function has no body — the return variable is declared but not initialised. Functions without a body must be marked 'as abstract'. Either provide an implementation or add 'as abstract'. See ek9 -h E07110 for details.

Incorrect:

    Formatter() as pure
      -> input as String
      <- output as String?

Correct:

    Formatter() as pure
      -> input as String
      <- output as String: input.trim()
Other ways to ask this
  • What triggers E07110 not abstract and no body provided?
  • How do I fix a method with no implementation in EK9?
  • Why does EK9 require explicit abstract keyword?

Coming from another language?

Java: interface methods are implicitly abstract, class methods without body are compile errors. Python: no enforcement, pass or raise NotImplementedError by convention. Rust: trait methods without body are implicitly abstract. Kotlin: abstract keyword required on both class and method. Go: interface methods have no body, struct methods must have body. EK9: explicit 'as abstract' required — no implicit abstractions.

Keywords: E07110, explicit, method, implement, intent, missing, concrete, function, body, abstract