Kotlin Class and Objects

In this article, you'll be introduced to Object-oriented programming in Kotlin. You'll learn what a class is, how to create objects and use it in your program.

Kotlin supports both functional and object-oriented programming.

Kotlin supports features such as higher-order functions, function types and lambdas which makes it a great choice for working in functional programming style. You will learn about these concept in later chapters. This article will focus on object-oriented style of programming in Kotlin.

Object-oriented Programming (OOP)

In object-oriented style of programming, you can divide a complex problem into smaller sets by creating objects.

These objects share two characteristics:

⚡️ state ⚡️ behavior

Kotlin Class

Before you create objects in Kotlin, you need to define a class. A class is a blueprint for the object.

We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object.

Since, many houses can be made from the same description, we can create many objects from a class.

How to define a class in Kotlin?

To define a class in Kotlin, class keyword is used:

class ClassName {
    // property
    // member function
    ... .. ...
}

Here's an example:

class Lamp {

    // property (data member)
    private var isOn: Boolean = false

    // member function
    fun turnOn() {
        isOn = true
    }

    // member function
    fun turnOff() {
        isOn = false
    }
}

Here, we defined a class named Lamp.

The class has one property isOn (defined in the same way as variable), and two member functions turnOn() and turnOff().

In Kotlin, either the property must be initialized or must be declared abstract. In the above example, isOn property is initialized to false.

Classes, objects, properties, member function etc. can have visibility modifiers. For example, the isOn property is private. This means, theisOnproperty can be changed from only inside the Lamp class.

Other visibility modifiers are:

private - visible (can be accessed) from inside the class only.

public - visible everywhere.

protected - visible to the class and its subclass.

internal - any client inside the module can access them.

If you do not specify the visibility modifier, it will be public by default.

In the above program, turnOn() and turnOff() member functions are public whereas, isOn property is private.

Kotlin Objects

When class is defined, only the specification for the object is defined; no memory or storage is allocated.

To access members defined within the class, you need to create objects. Let's create objects of Lamp class.

class Lamp {

    // property (data member)
    private var isOn: Boolean = false

    // member function
    fun turnOn() {
        isOn = true
    }

    // member function
    fun turnOff() {
        isOn = false
    }
}

fun main(args: Array<String>) {

    val l1 = Lamp() // create l1 object of Lamp class
    val l2 = Lamp() // create l2 object of Lamp class
}

This program creates two objects l1 and l2 of class Lamp. The isOn property for both lamps l1 and l2 will be false.

How to access members?

You can access properties and member functions of a class by using . notation. For example:

l1.turnOn()

This statement calls turnOn() function for l1 object.

Let's take another example:

l2.isOn = true

Here, we tried to assign true toisOn property of l2 object. Note that, isOn property is private, and if you try to accessisOn from outside the class, an exception is thrown.