Kotlin Class - Kotlin Constructor With Examples

In this tutorial, we’ll be discussing the Object Oriented Programming concepts of Kotlin. We’ll discuss Kotlin Class in detail. We will also look at kotlin constructors, access modifiers and abstract class.

Kotlin Class

A class is a blue print defined which groups functions and properties. Classes in Kotlin are defined using the keyword class followed by the class name. The body goes inside the curly braces.

An instance of the class is instantiated in the following way:

Contrary to Java, new isn’t a keyword in Kotlin.

Classes by default are final in Kotlin.
So the equivalent of the above definitions in Java would like something like this:

Hence, by default classes in Kotlin are not inheritable.

To make a class non-final we need to append the keyword open.

The open annotation allows others to inherit from this class.

Kotlin Class Example

Let’s create a class with a few functions and a property. We’ll see how to access the functions and properties of that class. Furthermore, we’ll see how to set the member properties.

The function main belongs to the Test.kt class. To access members and functions, we need to use the dot operator. A val property can’t be set again using a dot operator.

Kotlin init

Kotlin init block is defined as shown below.

The code inside the init block is the first to be executed when the class is instantiated. The init block is run every time the class is instantiated, with any kind of constructor as we shall see next.

Multiple initializer blocks can be written in a class. They’ll be executed sequentially as shown below.

Kotlin classes allow printing properties in the declaration itself by using the also function as shown below.

Kotlin Constructor

Kotlin Constructors are special member functions that are used to initialize properties. Constructors in Kotlin are written and structured differently compared with Java. By default a class has an empty constructor as shown below:

Primary Constructors

Primary Constructors in Kotlin are defined in the class header itself as shown below.

The primary constructors definition goes inside the class header. We’ve defined the property types(val/var) in the constructor itself.

Note: Unless stated as a var, by default, constructor arguments are val.

In the above code, both name and isAdmin can’t be reassigned.

Alternatively, we can aslo assign the constructor arguments to the member properties in the class as shown below.

Kotlin Constructor Default Values

Kotlin allows us to specify default values in the constructor itself as shown below.

Secondary Constructors

Secondary Constructors are written inside the body of the class by prefixing with the keyword constructor. Following example demonstrates the same.

The most common usage of secondary constructors comes in subclasses when you need to initialize the class in different ways.

If the class contains a primary constructor, the secondary constructor must refer to it in its declaration. The declaration is done using this keyword.

init block is used to initialise the member property skill. The secondary constructor delegates to the primary constructor using : this.

Custom Getter and Setters

Up until now we’ve accessed and modified properties in a class using the dot operator on the instance of the class. Let’s use set and get syntax to see how we can customise the access.

Following is printed in the log console:

The field variable in the setter saves the older value. Let’s add a getter.

capitalize() capitalizes the first letter of the string.

Note: if the property is a val, set method won’t compile.

Kotlin Visibility Modifier

  • Public : Any class, function, property, interface, or object that has this modifier is visible and can be accessed from anywhere.
  • Private: A class/function defined with this modifier can be only accessed within the same file. A member/property in a class/function with this modifier can be only accessed within that block.
  • Protected : This modifier is same as private, except that it allows visibility and access within subclasses.
  • Internal: A class/interface/function with this modifier is accessible only within the same module.

Visibility Modifiers are applicable on Constructors as well. Assigning a modifier to a Primary Constructor requires us to specify the keyword constructor alongside the constructor in the class header.

Private constructors can’t be called outside the class. In the above code, we can instantiate the class in a different function only using the secondary constructor.

Kotlin Abstract Class

Like Java, abstract keyword is used to declare abstract classes in Kotlin. An Abstract class can’t be instantiated. However, it can be inherited by subclasses. By default, the members of an abstract class are non-abstract unless stated otherwise.

Note: Abstract classes are by default open. So adding an open modifier to allow subclassing isn’t required.

override keyword is used to override a method in the subclass.

We’ve covered the basics of kotlin classes in this tutorial. There’s a lot more still there such as Data Classes, Sealed Classes, Inheritance etc. We’ll be covering them in the upcoming tutorials.

References : Kotlin Docs

By admin

Leave a Reply

%d bloggers like this: