Signup/Sign In

Kotlin Class and Object

In this tutorial we will start discussing about Object Oriented Programming in Kotlin.

There are several types of programming paradigms which different languages follows to provide different features. Major programming paradigms are:

  • Imperative

  • Logical

  • Functional

  • Object Oriented

The most popularly used paradigm is Object Oriented Programming. Kotlin also provides OOP features such as abstraction, encapsulation, inheritance, etc. in the form of classes, objects, interfaces, etc. But Kotlin is not a pure OOP language as it also provides Functional programming features like lambdas, higher order function, etc. So, Kotlin supports both functional and object-oriented programming.

Here is an amazing video explaining the basic OOPs concepts:

Now we will discuss about basics of OOPs: Classes and Objects

In real word, we may find different objects of same kind, but with different properties. Let us take example of a mobile. Each mobile has some properties like model no, price, colour, size, RAM, memory, etc. To be able to define different mobile configuration, we must have some sort of blank spaces to be filed, like a blueprint, using which we can create multiple Mobile devices objects. We can achieve this using a class.

The class is used to create a blueprint of an object. So let's continue with this example, below we will define a class named Mobile in which we will define all its properties and methods, and then we will create objects of that class, with different values for those properties. Conside the objects as mobile phones provided by different companies, or different models of mobiles, like iPhone 11 Pro can be one object, Samsung Galaxy S 20 can be another object with its own configuration, then One Plus 7, and so on.

Kotlin Class

A class is defined using the class keyword. The syntax for creating a class is:

class <class-name> // class header
{
    // class body
}

A Kotlin class has two main parts:

  • Class header: It contains class name, primary constructor, parent constructor, etc. (We will learn about constructors in upcoming tutorials)

  • Class body: The class body contains properties, methods, secondary constructors, init block, etc.

We will discuss each of the them in details in following tutorials.

Let us start with creating a simple class Mobile. It contains following properties and methods:

  • Properties:
    • Brand

    • Model

    • MRP

    • Discount

  • Methods:
    • getActualPrice(): It will return discounted price of mobile.

    • printDetails(): This function will print details of mobile.

class Mobile {
    // brand of type String
    var brand: String = ""
    // model of type String
    var model: String = ""
    // mrp of type float
    var mrp: Float = 0f
    // discount of type float
    var discount: Float = 0f

    fun getActualPrice():Float{
        return mrp - discount
    }

    fun printDetails(){
        println("Mobile details:")
        println("Brand: $brand")
        println("Model: $model")
        println("MRP: $mrp")
        println("Discount: $discount")

    }
}

We've created a basic class with properties and methods.

Important points regarding Kotlin classes are:

  • Properties must be initialised at the time of creation of object. We've initialised brand & model as empty string and mrp & discount to 0, as default values.

  • By default, classes are public in Kotlin.

Kotlin Class Object

Object is the real-world entity. It has all the properties and methods declared in class. The syntax to declare an object of a class is:

var varName = ClassName()

We can access the properties and methods of a class using the .(dot) operator. After the object name add . and the name of property/method:

var varName = ClassName()
varName.property = 0
varName.functionName()

Let us create an object of the Mobile class and call its methods:

fun main() {
    val mobile = Mobile()
    mobile.brand = "iPhone"
    mobile.model = "11 pro"
    mobile.mrp = 100000f
    mobile.discount = 1000f

    println("Discounted price is: ${mobile.getActualPrice()}")

    mobile.printDetails()
}


Discounted price is: 99000.0
Mobile details:
Brand: iPhone
Model: 11 pro
MRP: 100000.0
Discount: 1000.0

Explanation:

  • In line no 2, the object of class Mobile is created.

  • From line 3 to line 6, values to all the class properties are provided.

  • In line 8, the discounted price is printed using getActualPrice() function.

  • In line 10, printDetails() function is called.

Summary

In this tutorial we discussed about classes and objects. We created a basic class and created its object. In the next tutorial we will discuss about constructors.



About the author:
I'm a writer at studytonight.com.