Classes
Kotlin supports object-oriented programming with classes and objects. Objects are useful for storing data in your program. Classes allow you to declare a set of characteristics for an object. When you create objects from a class, you can save time and effort because you don't have to declare these characteristics every time.
To declare a class, use the class
keyword:
Properties
Characteristics of a class's object can be declared in properties. You can declare properties for a class:
Within parentheses
()
after the class name.
Within the class body defined by curly braces
{}
.
We recommend that you declare properties as read-only (val
) unless they need to be changed after an instance of the class is created.
You can declare properties without val
or var
within parentheses but these properties are not accessible after an instance has been created.
Just like with function parameters, class properties can have default values:
Create instance
To create an object from a class, you declare a class instance using a constructor.
By default, Kotlin automatically creates a constructor with the parameters declared in the class header.
For example:
In the example:
Contact
is a class.contact
is an instance of theContact
class.id
andemail
are properties.id
andemail
are used with the default constructor to createcontact
.
Kotlin classes can have many constructors, including ones that you define yourself. To learn more about how to declare multiple constructors, see Constructors.
Access properties
To access a property of an instance, write the name of the property after the instance name appended with a period .
:
Member functions
In addition to declaring properties as part of an object's characteristics, you can also define an object's behavior with member functions.
In Kotlin, member functions must be declared within the class body. To call a member function on an instance, write the function name after the instance name appended with a period .
. For example:
Data classes
Kotlin has data classes which are particularly useful for storing data. Data classes have the same functionality as classes, but they come automatically with additional member functions. These member functions allow you to easily print the instance to readable output, compare instances of a class, copy instances, and more. As these functions are automatically available, you don't have to spend time writing the same boilerplate code for each of your classes.
To declare a data class, use the keyword data
:
The most useful predefined member functions of data classes are:
Function | Description |
---|---|
| Prints a readable string of the class instance and its properties. |
| Compares instances of a class. |
| Creates a class instance by copying another, potentially with some different properties. |
See the following sections for examples of how to use each function:
Print as string
To print a readable string of a class instance, you can explicitly call the toString()
function, or use print functions (println()
and print()
) which automatically call toString()
for you:
This is particularly useful when debugging or creating logs.
Compare instances
To compare data class instances, use the equality operator ==
:
Copy instance
To create an exact copy of a data class instance, call the copy()
function on the instance.
To create a copy of a data class instance and change some properties, call the copy()
function on the instance and add replacement values for properties as function parameters.
For example:
Creating a copy of an instance is safer than modifying the original instance because any code that relies on the original instance isn't affected by the copy and what you do with it.
For more information about data classes, see Data classes.
The last chapter of this tour is about Kotlin's null safety.
Practice
Exercise 1
Define a data class Employee
with two properties: one for a name, and another for a salary. Make sure that the property for salary is mutable, otherwise you won’t get a salary boost at the end of the year! The main function demonstrates how you can use this data class.
Exercise 2
Declare the additional data classes that are needed for this code to compile.
Exercise 3
To test your code, you need a generator that can create random employees. Define a RandomEmployeeGenerator
class with a fixed list of potential names (inside the class body). Configure the class with a minimum and maximum salary (inside the class header). In the class body, define the generateEmployee()
function. Once again, the main function demonstrates how you can use this class.
- Hint 1
Lists have an extension function called
.random()
that returns a random item within a list.
- Hint 2
Random.nextInt(from = ..., until = ...)
gives you a randomInt
number within specified limits.