Why can't a constructor be abstract in EK9?

← Classes and OOP · Ref: Q774

Constructors create instances — that is their sole purpose. Marking a constructor 'abstract' says 'I have no implementation, a subclass must provide it'. But constructors MUST create an instance of their own class, so deferring that to a subclass is logically impossible.

WHY ABSTRACT CONSTRUCTORS ARE IMPOSSIBLE

When you call 'MyClass()', the constructor MUST create a MyClass instance. An abstract constructor would mean 'I do not create anything' — defeating the purpose. If you want polymorphic creation, use an abstract factory method instead.

THIS EXAMPLE

The Shape class is abstract with a concrete constructor that sets the name. The Circle subclass calls super() to initialise the base. Both constructors are concrete — they create their respective instances.

RELATED CONSTRUCTOR RULES

- E07050: abstract constructor (this error) — constructors must create instances
- E07060: override constructor — constructors are not inherited, so override is meaningless
- E07080: default constructor with parameters — default means no parameters

See Q89 for constructors. See Q768 for abstract methods. See Q86 for abstract classes.

Example

defines module qa.classesandoop.constructorabstract

  defines class

    Shape as abstract
      name <- String()

      Shape()
        -> n as String
        name :=: n

      area() as abstract
        <- rtn as Float?

      describe()
        <- rtn as String: name

      default operator ?

    Circle extends Shape
      radius <- Float()

      Circle()
        -> r as Float
        super("circle")
        radius :=: r

      override area()
        <- rtn as Float: radius * radius * 3.14159

      default operator ?

  defines program

    ShowShapes()
      stdout <- Stdout()
      circle <- Circle(5.0)
      if circle?
        stdout.println(circle.describe())

Common mistakes

E07050 — Making the constructor abstract (no body) is logically impossible — constructors must create instances. A constructor cannot defer creation to a subclass. Remove 'as abstract' and provide a body. See ek9 -h E07050 for details.

Incorrect:

      Shape() as abstract
        -> n as String

Correct:

      Shape()
        -> n as String
        name :=: n

E07050 — A constructor cannot be abstract. It must create an instance of its class. Remove 'as abstract' and provide the constructor body. See ek9 -h E07050 for details.

Incorrect:

      Circle() as abstract
        -> r as Float

Correct:

      Circle()
        -> r as Float
        super("circle")
        radius :=: r
Other ways to ask this
  • What triggers E07050 abstract constructor?
  • Can I defer construction to a subclass in EK9?
  • Why does EK9 reject abstract on constructors?

Coming from another language?

Java: abstract constructors are a compile error. Python: __init__ can be overridden but not marked abstract via abc. Rust: no constructors, new() is a convention. Kotlin: constructors cannot be abstract. Go: no constructors, factory functions used. EK9: constructors must be concrete, use abstract factory methods for polymorphic creation.

Keywords: override, default, class, constructor, impossible, factory, instance, create, E07050, abstract