Kotlin Classes/Objects

Kotlin Classes and Objects

As you learned in the previous chapter, a Class is a blueprint or template, and an Object is a specific instance built from that blueprint.

In this tutorial, you will learn how to create your own classes and instantiate objects from them in Kotlin!


1. Creating a Class

To create a class in Kotlin, use the class keyword followed by the name of the class. By convention, class names should always start with an uppercase letter (PascalCase).

Inside the class, you can define variables. Variables defined inside a class are called properties.

Syntax:

class ClassName {
  // Property declarations
  var propertyName: DataType = value
}

Let's create a class named Car with some basic properties:

class Car {
  var brand: String = "Ford"
  var model: String = "Mustang"
  var year: Int = 1969
}

Note: A class by itself doesn't do anything. It is merely a blueprint. To use it, we must create an Object of that class.


2. Creating an Object

Now that we have our Car class blueprint, let's build a car!

To create an object of a class, you declare a variable and assign it to the class name followed by parentheses ().

val myCar = Car()

In many other languages like Java or C++, you have to use the new keyword to create an object (e.g., Car myCar = new Car();). In Kotlin, the new keyword does not exist! You simply call the class like a function.


3. Accessing Class Properties

You can access and modify the properties of an object by using the dot syntax (.).

Let's create an object, access its properties to print them, and even change the values:

Accessing Properties Example

class Car {
  var brand = "Ford"
  var model = "Mustang"
  var year = 1969
}

fun main() { // Create an object of the Car class val c1 = Car()

// Access the properties and print them println(c1.brand) println(c1.model) println(c1.year)

// Modify a property c1.year = 2023 println("Updated Year: " + c1.year) }

Pro Tip: Even though c1 was declared using val (which makes the object reference constant), we can still change its internal properties like c1.year because the property itself was defined using var inside the class!


4. Creating Multiple Objects

The true power of classes is that you can use a single blueprint to create as many unique objects as you want!

Multiple Objects Example

class Car {
  var brand = ""
  var model = ""
  var year = 0
}

fun main() { // Create Object 1 val car1 = Car() car1.brand = "Toyota" car1.model = "Corolla" car1.year = 2020

// Create Object 2 val car2 = Car() car2.brand = "Tesla" car2.model = "Model 3" car2.year = 2022

// Print details for both cars println(car1.brand + " " + car1.model + " (" + car1.year + ")") println(car2.brand + " " + car2.model + " (" + car2.year + ")") }

While the above code works perfectly, assigning properties line-by-line is quite tedious. What if we could assign these values the exact moment the object is created?

We can! This is done using Constructors, which we will learn about in the very next chapter.


Exercise 1 of 2

?

Which keyword is used to declare a blueprint for objects in Kotlin?

Exercise 2 of 2

?

How do you correctly create an object of a class named `Person` in Kotlin?