Kotlin

Kotlin Constructor

A constructor is defined as a function in any programming language that is used to initialize an object of a class. However, the Kotlin programming language deals with these constructors slightly differently. It has multiple types of constructors that are used to serve different purposes. In this article, we will first briefly discuss the different types of constructors in Kotlin and the purposes for which they are used. After that, we will walk you through some examples of each type of Kotlin constructor in Ubuntu 20.04.

Types of Kotlin Constructors:

There are two basic types of constructors in the Kotlin programming language, i.e., primary constructor and secondary constructor. The primary constructor of Kotlin is the one that is defined along with the name of the class, whereas the secondary constructor of Kotlin is the one that is defined within the class. The primary constructor in Kotlin cannot contain any additional functionality other than initialization. On the other hand, the secondary constructor can act as a full-fledged function. However, the primary constructors are used more commonly in Kotlin than the secondary constructors. Another important thing to be noted over here is that a Kotlin class can only have one primary constructor, whereas it can have multiple secondary constructors.

Using the Primary Constructor in Kotlin in Ubuntu 20.04:

To learn the usage of the primary constructor in Kotlin in Ubuntu 20.04, you can take help from the following two examples:

Example # 1: Basic Usage of the Primary Constructor in Kotlin:

The primary constructor of Kotlin in its purest form is used to initialize the objects of a class. To understand this, you can use the Kotlin script shown below:

In this script, we have a class named “Book.” We have defined its primary constructor that only contains the name of the book and its serial number as its parameters. Then, in our “main()” function, we have created an instance of our class named “b1”. We have initialized this instance by calling the primary constructor of our class with the name of a random book and its serial number. Finally, we have printed the name and the serial number of our book on the terminal by using the “${b1.name}” and “${b1.serialNum}” notations with the “println” statements.

The following command was used to compile this script:

$ kotlinc constructor.kt

The command shown below was used to execute this script:

$ kotlin ConstructorKt

The output of our Kotlin script is shown in the following image:

Example # 2: Usage of the Primary Constructor with Default Initialization in Kotlin:

You might not pass any parameters to a constructor while calling it. However, if the primary constructor had been designed to accept some parameters, then doing this will generate compile-time errors. Therefore, to avoid these errors, you can have a default initialization for the primary constructor so that it can cater to the missing parameters. The Kotlin script shown below depicts this:

In this example, we have used the same “Book” class; however, this time, we have also assigned some default values to both the parameters of our primary constructor. Then, to see how this primary constructor works with the default initialization, we created two instances of our class within our “main()” function, i.e., b1 and b2. However, we have initialized “b1” with some random values, whereas we have not passed any parameter to “b2” while calling its constructor. Then, we have printed the parameters of the constructors for both of our objects on the terminal.

You can see in the following output that for our first object, the passed parameters were printed, whereas, for the second object, the default parameters were printed.

Using the Secondary Constructor in Kotlin in Ubuntu 20.04:

To understand the working of the secondary constructor in Kotlin in Ubuntu 20.04, you can take a look at the following two examples:

Example # 1: Basic Usage of the Secondary Constructor in Kotlin:

In this example, we will use the secondary constructor for initializing a class object. This is depicted in the Kotlin script shown below:

In this example, we have created the “Book” class; however, we have not defined any primary constructor for it. Then, within this class, we have defined the secondary constructor with the keyword “constructor” and have assigned to it two parameters named “name” and “serialNum.” Within the body of this constructor, we have printed the values of these parameters on the terminal. Then, in our “main()” function, we have simply created an instance of our class and passed some random parameters to it.

The following output shows that the secondary constructor can also be used to initialize an object of a class:

Example # 2: Usage of the Secondary Constructor with Default Initialization in Kotlin:

Now, we will use the secondary constructor in Kotlin with default initialization. For that, we have used the Kotlin script shown in the image below:

This example script is more or less the same as our first example of the secondary constructor. However, we have initialized our constructor with some default values. Then, within our “main()” function, we have created an instance of our class and have called its constructor without any parameters for printing the default values.

The output shown in the following image verifies that if no value is passed as the parameters of the secondary constructor, then their default value is printed provided that the default values are defined with the declaration of the secondary constructor.

Conclusion:

This guide was based on the constructors in Kotlin in Ubuntu 20.04. We first shared with you the different types of Kotlin constructors, i.e., primary and secondary, and the reason behind their existence. Afterward, we shared with you two examples of each of these categories of Kotlin constructors. These examples were meant to educate you more on the usage of each of these types of Kotlin constructors. The examples shared in this article have been intentionally kept simple to convey the basic gist of this article clearly.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.