E07176: a constructor cannot rely on the implicit super() - the super has no accessible no-argument constructor (it is 'private', or there is none) - call or provide an accessible initialising super constructor

Category: E07xxx Full resolution · Reported in phase: FULL_RESOLUTION · Classification: CONSTRUCTOR_USES_PRIVATE_SUPER · All error codes

What went wrong

A constructor relies on the implicit super(), but the super has no accessible no-argument constructor: it is 'private', or there is none. Every EK9 constructor chains to its super - either an explicit super(...), an explicit this(...) delegation, or (when neither is written) a hidden IMPLICIT super() to the super's no-argument constructor. A private constructor is not accessible to a sub-type, and a missing one cannot be called, so the base cannot be reached this way.

A super can have NO no-argument constructor because it is itself a sub-type of a base whose no-argument constructor is private: EK9 generates one only when the super has an accessible one, all the way down a chain.

How to fix it

  1. Write a constructor that calls an accessible initialising super(...) (e.g. Sub() -> ... super(name))
  2. Or make the base's no-argument constructor accessible (public/protected) if the base is safe to construct with no arguments
  3. Or initialise the base's properties at declaration so its default need not be made private

Common causes

  • The base declares 'default private Base()' (its properties are uninitialised, see E07175), so a sub-type constructor that relies on the hidden implicit super() is invalid
  • Assumed that adding a sub-type constructor removes the hidden implicit super()
  • it does not; every constructor still chains to a super constructor
  • Extending a sub-type of such a base: it has no no-argument constructor at all

See also

  • E07177: a sub-type with NO constructor, whose default cannot be generated because the super has no accessible no-argument constructor
  • E07175: default constructor must be 'private' when uninitialised properties exist
  • E06180: 'not accessible from this context' (an explicit super(...) to a private constructor)

Examples

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

Fails with E07176: a constructor cannot rely on the implicit super() - the super has no accessible no-argument constructor (it is 'private', or there is none) - call or provide an accessible initialising super constructor

#!ek9
defines module error.e07176.bad

  defines class

    Reading as open
      amount as Integer?

      default private Reading()

      Reading()
        -> initial as Integer
        this.amount: initial

      default operator ?

    Sample extends Reading
      units as String?

      //This constructor would run the private Reading() implicitly
      Sample()
        -> unitName as String
        this.units: unitName

      default operator ?

Fixed: Call an accessible super constructor that initialises the super

#!ek9
defines module error.e07176.good

  defines class

    Reading as open
      amount as Integer?

      default private Reading()

      Reading()
        -> initial as Integer
        this.amount: initial

      default operator ?

    Sample extends Reading
      units <- String()

      Sample()
        ->
          initial as Integer
          unitName as String

        super(initial)
        this.units: unitName

      default operator ?

Questions and answers

These Q&A entries show E07176 and its fix: