E07260: class methods can only be marked with the 'protected' access modifier in classes that are 'open'

Category: E07xxx Full resolution · Reported in phase: EXPLICIT_TYPE_SYMBOL_DEFINITION · Classification: METHOD_MODIFIER_PROTECTED_IN_CLOSED_CLASS · All error codes

What went wrong

You've marked a method 'protected' in a class that isn't 'open'. Protected only makes sense for inheritance, which requires 'open'.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 1317 (Why can't I mark a method 'protected' in a closed clas...)

How to fix it

  1. Mark class as 'open' if inheritance is intended
  2. Or use 'private' if no inheritance needed

Common causes

  • Forgot to mark class as 'open'
  • Using protected when private would suffice

See also

  • E07260: protected in non-open class (this error)
  • E07270: access modifier on trait method

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E07260: class methods can only be marked with the 'protected' access modifier in classes that are 'open'

#!ek9
defines module error.e07260.bad

  defines class

    //Closed by default: there are no sub-classes to protect a method for
    Greeter
      greet()
        <- rtn as String: salutation()

      protected salutation()
        <- rtn as String: "hello"

Fixed: Make it private, or open the class

#!ek9
defines module error.e07260.good

  defines class

    //Option 1: keep it closed, and make the method private
    Greeter
      greet()
        <- rtn as String: salutation()

      private salutation()
        <- rtn as String: "hello"

    //Option 2: open the class, so sub-classes may use the protected method
    OpenGreeter as open
      greet()
        <- rtn as String: salutation()

      protected salutation()
        <- rtn as String: "hello"

Questions and answers

These Q&A entries show E07260 and its fix: