TExcelTAD
Home › Kotlin › Kotlin Classes/Objects

Kotlin Classes and Objects

Kotlin is an object-oriented language. A class is a blueprint that describes what some kind of thing has (its properties) and what it can do (its functions, also called methods). An object is a concrete thing built from that blueprint.

Class (blueprint) Objects (instances)
Car your blue hatchback, the neighbour's red van
Book Dune, Emma, The Hobbit
Account account #1001, account #1002

Defining a class

Use the class keyword. Properties are declared with val or var just like variables:

kotlin · try it yourself
class Book {
    var title = ""
    var author = ""
    var pages = 0
}

fun main() {
    val book = Book()        // create an object - no "new" keyword
    book.title = "The Hobbit"
    book.author = "J. R. R. Tolkien"
    book.pages = 310

    println("${book.title} by ${book.author}, ${book.pages} pages")
}
The Hobbit by J. R. R. Tolkien, 310 pages

Note that Kotlin has no new keyword: calling the class name like a function creates an instance. You access properties with a dot.

Several objects from one class

Each object has its own copy of the properties:

kotlin · try it yourself
class Counter {
    var count = 0
}

fun main() {
    val a = Counter()
    val b = Counter()
    a.count += 3
    b.count += 10
    a.count++
    println("a = ${a.count}, b = ${b.count}")
}
a = 4, b = 10

The primary constructor

Setting every property after creation is clumsy. Usually you declare the properties right in the class header — the primary constructor. Adding val/var to a parameter turns it into a property:

kotlin · try it yourself
class Book(val title: String, val author: String, var pages: Int)

fun main() {
    val b1 = Book("Dune", "Frank Herbert", 412)
    val b2 = Book("Emma", "Jane Austen", 474)

    println("${b1.title}: ${b1.pages} pages")
    println("${b2.title} by ${b2.author}")
}
Dune: 412 pages
Emma by Jane Austen

That single line declares a class with three properties and a constructor that fills them — what Java needs fifteen lines for. The next chapter explores constructors in depth.

Objects and equality

Two objects of a normal class are only == if they are the same object, even if all their properties match. (Data classes change this — see later.)

kotlin · try it yourself
class Point(val x: Int, val y: Int)

fun main() {
    val p1 = Point(1, 2)
    val p2 = Point(1, 2)
    val p3 = p1
    println(p1 == p2)
    println(p1 == p3)
}
false
true

Printing such an object with println(p1) shows the default toString(): the class name followed by a hash code, like Point@1b6d3586, which differs from run to run. You'll learn to override toString() in the Class Functions chapter, and data classes generate a readable one for you.

Type checks

The is operator checks whether an object is an instance of a class. After a successful check, the compiler smart-casts the variable:

kotlin
val item: Any = Book("Dune", "Frank Herbert", 412)
if (item is Book) println(item.title)   // smart-cast to Book

Tip: Classes are public and final by default in Kotlin. "Final" means other classes can't inherit from them unless you mark them open — covered in the Inheritance chapter.

Test yourself

How do you create an instance of class Dog?

  1. new Dog()
  2. Dog()
  3. Dog.new()
  4. create Dog

What does val in class User(val name: String) do?

  1. Nothing
  2. Makes name a read-only property of the class
  3. Makes the class immutable
  4. Declares a local variable

For a regular (non-data) class, when is a == b true?

  1. When all properties are equal
  2. When a and b are the same object (unless equals is overridden)
  3. Always
  4. Never