Kotlin Class and Objects

In this article, you will be introduced with the basic OOPs concept i.e. Classes and Objects and how you can create classes and objects in your Kotlin program.

Kotlin supports both functional and object-oriented programming.

Kotlin supports features such as higher-order functions, function types, and lambdas which represent Kotlin as a functional language. We will learn about these concepts in later chapters.

In this article, we will focus on the basic object-oriented programming concept (OOPs) ie. Classes and Objects.

Kotlin Objects

Object refers to an entity that has state and behavior.

State: represents the data (properties) of an object.
Behavior: represents the behavior (functionality) of an object.

Let’s take few examples:

1 . car is an object

It has model, color, weight, price as states.
speedup, changing gears, driving, etc. behavior.

2 . account is an object

It has account no, type, balance as states.
deposit, withdrawal, etc, are its behavior.

Kotlin Class

A Class is a way to bind the data (properties) describing an object and its associated functions together.

Classes are needed to represent real-world entities that not only have properties (their characteristics) but also have associated operations (their behavior).

How to define a class in Kotlin?

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

Syntax:

The syntax of defining a class in Kotlin:

class ClassName {
    //property
    //member function
}

In Kotlin, either the property must be initialized or must be declared abstract.

ClassName should use UpperCamelCase. For example, ClassAndObjectsDemo.

Example: Kotlin class

class Account {
    //properties
     private var type: String = ""
     private var accountNo: Int = 0
     private var balance: Float = 0f
    //member functions
    fun deposit(amount: Float) {
        balance = balance + amount
    }

    fun withdraw(amount: Float) {
        balance = balance - amount
    }
}

Example explained

  • The data describing an account (i.e., account no, type and balance) and its associated operations (deposit and withdraw) are bound together under one name Account (ClassName).

The keyword private in the above example, is an access modifier which we will discuss in detail in later chapters. For now, just remember:

  • The private keyword makes properties and member functions private which can only be accessed inside the same class.
  • In Kotlin, by default properties and member functions are public (no need to write public keyword) which can be accessed from outside the class.

Creating Objects

  • To access members defined within the class, you need to create objects.
  • You can create multiple objects of the same class.

Syntax:

The syntax of creating an object of a class:

var obj = className()

Example: Creating objects of a class

class Account {

     var type: String = ""
     var accountNo: Int = 0
     var balance: Float = 0f

    fun deposit(amount: Float) {
        balance = balance + amount
    }

    fun withdraw(amount: Float) {
        balance = balance - amount
    }
}
//account1 and account2 are two objects of class Account
fun main(args: Array<String>) {
    var account1 = Account()
    var account2 = Account()
}

How to access class members?

You can access class members (call functions and access properties) by using the dot(.) operator.

For example, member functions of a class can be accessed as :

account.deposit()

This statement calls the deposit() method inside the Account class for an account object.

When you call the function using the above statement, all statements within the body of deposit() function are executed. Then, the control of the program jumps back to the statement following account.deposit().

Similarly, the property of a class can be accessed as:

account.balance

 

Example: Kotlin Class and Objects

class Student {

    //properties
    var rollno: Int = 0
    var name: String = ""

    //member functions
    fun insertRecord(roll_no: Int, student_name: String) {
        rollno = roll_no
        name = student_name
    }

    fun displayDetail() {
        println("$rollno $name")
    }
}

    fun main(args: Array<String>) {
       //s1 and s2 are two objects of Student class
        var s1 = Student()
        var s2 = Student()
        s1.insertRecord(101, "Manish")
        s2.insertRecord(202, "Arun")
        s1.displayDetail()
        s2.displayDetail()
    }

Output

101 Manish
202 Arun

Example explained

In the above example,

  • Student class consists of two properties rollno, name and two member functions insertRecord() and displayDetail().
  • Inside main() function, s1 and s2 are two objects of Student class.
  • Here, we are initializing the value to these objects by invoking the insertRecord() method.
  • And, we are displaying the state (property) of the objects by invoking the displayDetail() method.

Leave a Reply