Sling Academy
Home/Kotlin/Defining Classes in Kotlin: The Basics

Defining Classes in Kotlin: The Basics

Last updated: November 30, 2024

In Kotlin, defining a class is straightforward and quite similar to other statically typed languages. A class is a blueprint for creating objects, encapsulating data for the object and functions to manipulate that data, known as properties and methods, respectively.

Basic Class Structure

To define a class in Kotlin, use the class keyword followed by the class name. Classes can contain properties, methods, and constructors.

Here's an example of a simple class in Kotlin:

class Car {
    var color: String = "unknown"
    var engineRunning: Boolean = false

    fun start() {
        engineRunning = true
    }

    fun stop() {
        engineRunning = false
    }
}

Class Properties

Properties in Kotlin are declared inside a class and describe the attributes of the class. They are defined using var (for mutable properties) or val (for read-only properties).

In the above example, color and engineRunning are properties of the Car class.

Methods in Classes

Methods are functions that perform actions using the properties of a class. They are defined using the fun keyword.

In the Car class, start and stop are methods that modify the state of the engineRunning property.

Constructors

Constructors are a special block of code used to initialize the state of an object. In Kotlin classes, the primary constructor is part of the class header.

Here’s how you can define a class with a primary constructor:

class Car(val color: String, val model: String) {
    var engineRunning: Boolean = false

    fun start() {
        engineRunning = true
    }

    fun stop() {
        engineRunning = false
    }
}

In this example, Car has a primary constructor with color and model as parameters, which are also properties of the class.

Creating an Object

Once a class is defined, you can create objects using the new keyword. However, in Kotlin, you simply call the constructor:

fun main() {
    val myCar = Car("Red", "Toyota")
    myCar.start()
    println("The car color is: "+ myCar.color)
    println("Engine running: " + myCar.engineRunning)
}

This will create a Car object. You can then access its methods and properties through the object instance, myCar.

Kotlin allows you to encapsulate functionality effectively using classes, making your code modular and clear. With these basics down, you are well on your way to efficiently designing object-oriented solutions in Kotlin.

Next Article: Understanding Objects in Kotlin: How to Create and Use Them

Previous Article: Introduction to Object-Oriented Programming in Kotlin

Series: Kotlin Object-Oriented Programming

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin