Authors Top

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Introduction

While using Kotlin, we often stumble upon the following warning/error:

'super' is not an expression, it can only be used on the left-hand side of a dot ('.')

This error may have occurred while trying to invoke the constructor of a base class:

open class Vehicle()
class Car() : Vehicle() {
    init {
        super() //compilation error
    }
}

That is a perfectly valid expression to invoke the primary constructor of a base class in Java. However, in Kotlin, this expression is invalid.

Let’s look at how to properly invoke base class constructors in Kotlin. Later, we’ll also look at other uses of the super keyword in Kotlin.

2. Calling Base Class Constructors

Interestingly, the primary base constructor can be implicitly called in Kotlin. We don’t need to use the super keyword at all:

open class Vehicle()
class Car() : Vehicle() {}

As we can see, the call to the primary constructor of the base Vehicle class is part of the class header itself.

This leads to an implicit call to the base class constructor from the constructor of the derived class. This means we don’t need to use the super keyword to invoke it explicitly.

3. super Keyword in Kotlin

Even though the super keyword isn’t needed in order to delegate to base class primary constructors, it is, in fact, a valid keyword in Kotlin. There are multiple other uses for it. Let’s take a look at them.

3.1. Secondary Constructor Delegation With super

Firstly, we can use the super keyword to delegate constructor calls — specifically, calls to the base class constructors from secondary constructors of a derived class:

open class Vehicle() {
    constructor(name: String) : this() {}
}
class Motorbike : Vehicle {
    constructor(name: String) : super(name) {}
}

Note that this is only possible in certain scenarios. Firstly, the derived class must not have declared a primary constructor. Secondly, the base class must have declared a non-default constructor (primary or secondary).

3.2. Overridden Function Invocation With super

Additionally, we can use the super keyword to invoke methods declared in the base class:

open class Vehicle() {
    open fun start() {}
}

class Motorbike : Vehicle() {
    override fun start() {
        super.start()
    }
}

This is especially useful in cases where the derived class has overridden a method from the base class. We can specify the invocation of the base class version of that method using the super keyword.

Notably, this particular behavior of the super keyword in Kotlin is very similar to how it behaves in Java.

4. Conclusion

In this article, we discussed various ways to use the super keyword in the Kotlin language. We can use it to delegate constructor calls.

Additionally, we can invoke overridden functions in the base class with super, similar to how it’s done in Java.

As always, code samples can be found over on GitHub.

Authors Bottom

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!