Table of contents
1.
Introduction
2.
Kotlin Class
2.1.
Syntax of Kotlin class declaration
2.2.
Example of Kotlin class
3.
Kotlin Object
3.1.
Create an object
3.2.
Access class property and member function
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Kotlin Classes and Objects

Author Vasu Bansal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, you will learn about classes and objects in Kotlin. Kotlin, like other programming languages, supports basic programming paradigms like Object Oriented Programming (OOP) and functional programming. The concept of OOPs is derived from the idea of objects and classes. With the use of classes and objects in Kotlin, one can achieve the fundamental principles of OOPs like abstraction, encapsulation, inheritance and polymorphism. In the next section, we will study about Kotlin Classes.

Kotlin Class

Before diving deep, let us understand the meaning of the term class. A class is a template for objects with similar attributes. Kotlin classes are identical to the class definition in Java and are declared using the keyword class.   

Syntax of Kotlin class declaration

A Kotlin class has a class header that describes the type of arguments, constructors, and a class body enclosed by curly brackets. To learn more about Kotlin constructors, refer to the blog Kotlin Constructors on the Coding Ninjas Website.

class TestClass{   // class header  
    // class variables and member function with appropriate access modifiers  
}  

Example of Kotlin class

The following is an example of a Kotlin class. The class name is Student, and it has a secondary constructor that initialises two of its arguments, i.e. ninja_id and student_name. The class also contains two member functions named get_course_list and get_CGPA, which do their respective work.

Code:

// Kotlin program to demonstrate class syntax with example

class Student {
    constructor (ninja_id: Int, student_name: String) {
        println("Class Constructor --> Ninja ID: ${ninja_id} and Student Name: ${student_name}")
    }

    fun get_course_list() {  
        // Get all the courses in which the student with given ninja_id is enrolled in
    }  
 
    fun get_CGPA() {  
       // Get CGPA of the student with given ninja_id  
    }  
}

Kotlin Object

Objects are the core elements of Object-Oriented Programming. Objects in programming also have correspondence to real-life entities. Objects are the ways to access the member properties and function of the class to which that object belongs. Like in most programming languages, one can create multiple objects of the class in Kotlin as well. The following are the components of an object.

State: It is represented by an object's characteristics. It also reflects an object's attributes.

Behaviour: It is represented via an object's methods. It also reflects an object's interaction with other objects.

Identity: It gives an object a unique name and allows it to communicate with other objects.

Create an object

The reference of the class can be used to create an object. It is actually a two-step process where the first step is the create a reference and the second step is to create an object. For example, the following syntax can be used to create an object of a class named TestClass.

var obj = TestClass() 
// It calls the default constructor of the class TestClass if no custom constructor is specified

TestClass() is an object and obj is a reference. Note that we can create multiple objects of the same class. For example, the following snippet will also run perfectly.

// Creating multiple objects of the same class
var test_obj_1 = TestClass()
var test_obj_2 = TestClass()

Access class property and member function

The . operator can be used to access a class's member functions and properties. The following program will show the usage of this operator.

Code:

class Student {
    var ninja_id: Int = 0
    var ninja_name: String = ""

    fun add_details(ninja_id: Int, ninja_name: String){
        this.ninja_id = ninja_id
        this.ninja_name = ninja_name
    }  
}

fun main(){
    var obj = Student()

    obj.add_details(1, "Code Studio")

    println("Ninja ID: ${obj.ninja_id}")
    println("Ninja Name: ${obj.ninja_name}")
}

Output:

Ninja ID: 1
Ninja Name: Code Studio

 

Know What is Object in OOPs here in detail.

FAQs

  1. What are companion objects?
    Companion objects are declared inside a class, and they help access its members using only the class name as a qualifier.
     
  2. What are the differences between class and data class in Kotlin?
    A data class is a state-only object that doesn't perform any operation. The advantage of utilising data classes instead of ordinary classes is that Kotlin generates a lot of self-generated code for us.
     
  3. What is the use of inner class in Kotlin?
    A class that is created inside another class is called the inner class. Inner classes are declared using the keyword inner. An inner class cannot be declared inside interfaces or non-inner nested classes.

Key Takeaways

Cheers if you reached here!! This blog aimed to introduce you to the concept of classes and objects in Kotlin. Go ahead and read the blog Kotlin Visibility Modifiers to learn the various types of visibility modifiers that can be applied to classes and properties in Kotlin.

Yet there is never an end to the learning process, so check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development and how to design the applications of future. Until then, good luck!!

Live masterclass