Lesson Overview

Greetings! Today, we're exploring Kotlin classes, the core building blocks of Object-Oriented Programming (OOP) in Kotlin. Through hands-on examples, we'll dive into the fundamental concepts of Kotlin classes, including their structure, properties, and functions.

Kotlin Classes Refresher
Structure of a Kotlin Class

A Kotlin class serves as a blueprint consisting of properties and functions. While properties represent data relevant to a class instance, functions are actions or operations that manipulate this data. Each class may use a primary constructor to define properties directly.

Kotlin's primary constructor allows you to define properties concisely without requiring additional getters or setters, as they are automatically generated by the language. This concise syntax eliminates boilerplate code often needed in other programming languages, making your classes more readable and easier to maintain.

class GameCharacter(val name: String, var health: Int, var strength: Int)

fun main() {
    val character = GameCharacter("Hero", 100, 20)  // Object or instance of the class
}

In the example above, the name, health, and strength properties are defined directly in the primary constructor and can be accessed or modified without writing additional boilerplate code. This demonstrates Kotlin's powerful and concise syntax for defining classes.

Class Properties

Properties in Kotlin classes hold data associated with each class instance. For example, in our GameCharacter class, name, health, and strength are properties. You can access a class property using the object of the class, followed by a dot (.), and then the property name.

class GameCharacter(val name: String, var health: Int, var strength: Int)

fun main() {
    val character = GameCharacter("Hero", 100, 20)  // object or instance of the class
    println(character.name)  
    println(character.health)  
    println(character.strength) 
}

/* Output:
 Hero
 100
 20
 */

Properties differentiate one class instance from another and store the state of the instance.

Class Functions

A class also contains functions — actions or operations that manipulate the data in the class. For example, the attack function in our GameCharacter class simulates an attack by one game character on another.

In Kotlin, class functions can interact with the class's own properties or with properties of other class instances. An essential keyword within these functions is this, which represents the current class instance. It allows access to the class's properties and functions, providing clarity in situations where property names overlap with function parameters.

When a new class instance is created, Kotlin automatically manages the this reference, enabling interaction with the instance's properties and functions.

class GameCharacter(val name: String, var health: Int, var strength: Int) {
    // Function to simulate an attack on another character
    fun attack(otherCharacter: GameCharacter) {
        otherCharacter.health -= this.strength  // Reduces the health of the target character
    }
}

fun main(){
    val character1 = GameCharacter("Hero", 100, 20)   // First instance of GameCharacter
    val character2 = GameCharacter("Villain", 80, 15)  // Second instance

    println(character2.health)  
    character1.attack(character2)  
    println(character2.health) 
}

/* Output:
80 
60
*/
Examples of Kotlin Classes, Properties, and Functions
Lesson Summary and Practice

Great work exploring Kotlin classes, their properties, and functions. Kotlin classes help organize your code, improving its readability and manageability. Now, test your understanding with exercise problems to solidify your newly acquired knowledge. Happy coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal