Kotlin Classes

Classes and objects form the core of object-oriented programming. Kotlin classes are simple and concise compared to classes in other programming languages. Read my post about object-oriented programming to get an idea about what are classes and objects.

kotlin classes

So, the class is the mold and we create objects using that mold. Classes are also called blueprints of objects. In Kotlin, we define classes using the keyword class. It is followed by the class name.

class classname {
property1
property2
.
.
fun function1()
fun function2()
.
.
}

Actually, it’s not always necessary to have a body for the classes. We can define classes in one line, without a body. We’ll see more about it when we learn constructors.

We create objects with the syntax:

var/val object_name = classname(parameters)

We define a class as the mold and we create objects using that mold. Objects have both properties and functions associated with them.

Simple class example

Let’s define a simple class that contain some properties only.

class data1 {
    var prop1: Int = 0
    var prop2: Int = 1
}

fun main(args: Array<String>) {
    val obj1 = data1()
    val obj2 = data1()

    println(obj1.prop1)
    println(obj1.prop2)
    println(obj2.prop1)
    println(obj2.prop2)

    obj1.prop1 = 45
    obj1.prop2 = 52

    obj2.prop1 = 23
    obj2.prop2 = 61

    println(obj1.prop1)
    println(obj1.prop2)
    println(obj2.prop1)
    println(obj2.prop2)
}

We defined a class named data1. In it, there are two properties: prop1 and prop2. In the main function, we created two objects of the class data1. The objects are obj1 and obj2. Even though the objects are of the same type, they are not related to each other. It is like different balls made from the same mold.
We use the dot (.) operator to access the properties of objects. We can assign new values and print them as we want.

Classes with properties and functions

Now let’s see a class with both properties and functions associated with it.

class data1 {
    var prop1: Int = 0
    var prop2: Int = 1
    fun sum(): Int = prop1 + prop2
    fun printValues() {
        println("Property1: $prop1")
        println("Property1: $prop2")
    }
}

fun main(args: Array<String>) {
    val obj1 = data1()

    obj1.prop1 = 28
    obj1.prop2 = 54
    println(obj1.prop1)
    println(obj1.prop2)
    println(obj1.sum())
    obj1.printValues()
}

There are two properties and two functions defined in the class. The object created will have these two properties and two functions. The function is in the class, so, it can access the properties of the class without a dot operator. The first function sum() is a single line function. The second has a function body enclosed in curly braces. It simply prints the two properties of the object when called.

I hope you got a basic idea about classes in Kotlin, how to define them and how to create objects of a class. The concept will become clear only when you practice it over and over again.
Hope you enjoyed it.
I’ll write about Kotlin constructors in the next post.
Happy coding đŸ™‚

« Back to the index of Kotlin tutorial

Add a Comment

Your email address will not be published. Required fields are marked *