Skip to main content
St Louis

Back to all posts

How to Create A Class In Kotlin?

Published on
4 min read
How to Create A Class In Kotlin? image

Best Kotlin Programming Books to Buy in September 2025

1 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
2 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
3 Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

BUY & SAVE
$33.00 $38.99
Save 15%
Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language
4 Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

BUY & SAVE
$59.30 $89.99
Save 34%
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

  • EXCEPTIONAL QUALITY ASSURANCE FOR ULTIMATE CUSTOMER SATISFACTION.
  • COMPETITIVE PRICING WITH UNMATCHED VALUE FOR YOUR INVESTMENT.
  • USER-FRIENDLY DESIGN FOR EFFORTLESS EXPERIENCE AND ACCESSIBILITY.
BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
+
ONE MORE?

To create a class in Kotlin, you need to follow a simple syntax. Here's the basic structure of a class in Kotlin:

class ClassName { // Properties (variables) and functions go here }

The class declaration starts with the keyword class, followed by the name of the class. Inside the curly braces, you can define the properties (variables) and functions of the class.

Properties can be defined using the var or val keywords, where var indicates a mutable variable and val indicates an immutable (read-only) variable. For example:

class Person { var name: String = "" val age: Int = 0 }

In the above example, name is a mutable variable of type String, and age is an immutable variable of type Int.

Functions can be defined within the class as well. Here's an example of a class with a function:

class Calculator { fun add(num1: Int, num2: Int): Int { return num1 + num2 } }

In the above example, add is a function that takes two parameters num1 and num2 of type Int and returns their sum.

You can create instances of the class by using the ClassName() syntax, and then access its properties or call its functions using the dot notation. Here's an example:

val person = Person() person.name = "John" person.age = 25 // This will give a compilation error as age is read-only

val calculator = Calculator() val sum = calculator.add(5, 3) // Returns 8

That's a brief overview of how to create a class in Kotlin and define properties and functions within it.

How to implement an interface in a Kotlin class?

To implement an interface in a Kotlin class, you need to use the : InterfaceName syntax after the class name. Here is an example implementation of an interface in a Kotlin class:

interface MyInterface { fun myFunction() }

class MyClass : MyInterface { override fun myFunction() { println("Implementing myFunction in MyClass") } }

In the above example, the MyClass implements the MyInterface by using the : MyInterface syntax after the class name. It also overrides the myFunction method defined in the interface.

You can then create an instance of the MyClass and call the myFunction method as shown below:

fun main() { val myClass = MyClass() myClass.myFunction() }

This will output: "Implementing myFunction in MyClass"

How to create inner classes in Kotlin?

In Kotlin, you can create inner classes using the inner keyword. An inner class is a class that is nested inside another class and has access to its outer class's members. Here's how you can create inner classes in Kotlin:

class Outer { private val outerProperty = "Outer Property"

inner class Inner {
    fun innerMethod() {
        println(outerProperty) // Accessing outer class property
    }
}

}

In the example above, the Inner class is an inner class of the Outer class. It has access to the outerProperty property of the Outer class.

To create an instance of the inner class, you need an instance of the outer class. Here's how you can do it:

val outer = Outer() val inner = outer.Inner() // Creating an instance of the inner class inner.innerMethod() // Accessing inner class method

In this example, we create an instance of the Outer class and then use that instance to create an instance of the Inner class. Finally, we call the innerMethod() of the Inner class.

Note that inner classes hold a reference to an instance of their outer class. If you want to create a nested class without the reference to the outer class, you can use the nested keyword instead of inner.

How to create a sealed class in Kotlin?

To create a sealed class in Kotlin, follow these steps:

  1. Define the sealed class using the sealed keyword. Sealed classes are used to represent restricted class hierarchies where all the subclasses are known. They are useful when you have a fixed set of subclasses and want to enforce that no other subclasses can be added.

sealed class MySealedClass { // subclasses will be defined here }

  1. Inside the sealed class, define the subclasses using the class keyword. Subclasses of a sealed class must be declared within the same file as the sealed class itself (or in an extending class).

sealed class MySealedClass { class Subclass1 : MySealedClass() class Subclass2 : MySealedClass() // additional subclasses }

  1. Optionally, you can also define object expressions as subclasses of the sealed class.

sealed class MySealedClass { class Subclass1 : MySealedClass() class Subclass2 : MySealedClass()

object Subclass3 : MySealedClass() {
    fun someFunction() {}
}

}

Note: The subclasses of a sealed class can have their own properties and methods.

Now you have created a sealed class MySealedClass with subclasses Subclass1, Subclass2, and an object expression Subclass3.