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:
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:
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:
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.)
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:
val item: Any = Book("Dune", "Frank Herbert", 412)
if (item is Book) println(item.title) // smart-cast to BookTip: Classes are
publicandfinalby default in Kotlin. "Final" means other classes can't inherit from them unless you mark themopen— covered in the Inheritance chapter.
Test yourself
How do you create an instance of class Dog?
- new Dog()
- Dog()
- Dog.new()
- create Dog
What does val in class User(val name: String) do?
- Nothing
- Makes
namea read-only property of the class - Makes the class immutable
- Declares a local variable
For a regular (non-data) class, when is a == b true?
- When all properties are equal
- When a and b are the same object (unless equals is overridden)
- Always
- Never