Compatibility with Java hierarchies that demand multiple constructors, such as
With a primary constructor:
class Foo(a: Bar): MySuper() { // when there's a primary constructor, (direct or indirect) delegation to it is required constructor() : this(Bar()) { ... } // can't call super() here constructor(s: String) : this() { ... } }
No primary constructor:
class Foo: MySuper { // initialization of superclass is not allowed constructor(a: Int) : super(a + 1) { ... } // must call super() here }
No primary constructor + two overloaded constructors
class Foo: MySuper { // initialization of superclass is not allowed constructor(a: Int) : super(a + 1) { ... } constructor() : this(1) { ... } // either super() or delegate to another constructor }
No primary constructor => no supertype initialization allowed in the class header:
class Foo : Bar() { // Error constructor(x: Int) : this() {} }
When a primary constructor is present, explicit constructors are called secondary.
Every class must have a constructor. The following is an error:
class Parent class Child : Parent { }
The error is: “superclass must be initialized”. This class has a primary constructor, but does not initialize its superclass in teh class header.
constructor : modifiers "constructor" valueParameters (":" constructorDelegationCall) block ; constructorDelegationCall : "this" valueArguments | "super" valueArguments ;
Passing lambdas outside parentheses is not allowed in constructorDelegationCall
.
The only situation when an explicit constructor may not have an explicit delegating call is
class Parent {} class Child: Parent { constructor() { ... } // implicitly calls `super()` }
If there's a primary constructor, all explicit constructors must have explicit delegating calls that (directly or indirectly) call the primary constructor.
class Parent {} class Child(): Parent() { constructor(a: Int) : this() { ... } }
The primary constructor's body consists of
val
or var
If the primary constructor is not present, property initializers and anonymous initializers are conceptually “prepended” to the body of each explicit constructor that has a delegating call to super class, and their contents are checked accordingly for definite initialization of properties etc.
Anonymous initializer in the class body must be prefixed with the init
keyword, without parentheses:
class C { init { ... // anonymous initializer } }
All constructors must be checked for
No secondary constructors can be declared for
Data classes should have a primary constructor