Classes
Classes are the building blocks of object-oriented programming. They are a system for organizing and reusing code. Think of a class as a blueprint for an object. It defines the properties and behaviors of the object, but it is not the object itself. When you create an object from a class, you are creating an instance of that class. The object has its own unique properties and behaviors, but it is based on the blueprint defined by the class.
Defining a Class
To define a class in Kotlin, you use the class
keyword followed by the class name. Here is an example of a simple
class definition:
class Person {
var name: String = ""
var age: Int = 0
}
In this example, we define a class called Person
with two properties: name
and age
. The properties are defined
using the var
keyword, which indicates that they are mutable (can be changed). The properties are initialized with
default values of an empty string and zero, respectively.
Creating Objects
To create an object from a class, you use the class name almost like a function call. Here is an example of creating a
Person
object:
val person = Person()
In this example, we create a Person
object and assign it to a variable called person
. The object is created using
the default constructor of the Person
class, which does not take any arguments. We will learn more about
constructors in the next lesson.
Properties / Fields
Properties are variables that are associated with an object. They define the state of the object and can be accessed and
modified from outside the class. In the Person
class, name
and age
are properties. These are unique to each
instance of the class. For example, you can create multiple Person
objects with different names and ages.
Accessing Properties
You can access the properties of an object using the dot notation. Here is an example of accessing the name
property
of a Person
object:
val person = Person()
person.name = "Alice"
println(person.name) // Output: Alice
In this example, we create a Person
object and assign the name "Alice" to the name
property. We then print the value
of the name
property using the println
function.
Changing Properties
You can change the value of a property by assigning a new value to it. Here is an example of changing the age
property of a Person
object:
val person = Person()
person.age = 30
println(person.age) // Output: 30
In this example, we create a Person
object and assign the age 30 to the age
property. We then print the value of the
age
property using the println
function.
Getters and Setters
In Kotlin, you can define custom behavior for getting and setting properties using getters and setters. Getters are methods that are called when you access a property, and setters are methods that are called when you assign a value to a property.
Here is an example of defining custom getters and setters for the name
property of the Person
class:
class Person {
var name: String = ""
get() = field.toUpperCase()
set(value) {
field = value.trim()
}
}
In this example, we define a custom getter for the name
property that converts the name to uppercase. We also define a
custom setter that trims leading and trailing whitespace from the name. When you access or assign a value to the name
property, the custom getter and setter are called.
Property Visibility
By default, properties in Kotlin are public, which means they can be accessed and modified from outside the class. You
can also specify the visibility of properties using access modifiers. The available access modifiers are public
,
protected
, private
, and internal
. But you mainly use public
and private
, so we will focus on those.
public
: The property is accessible from anywhere.private
: The property is accessible only from within the class.
Both are pretty simple to understand. If you want a property to be accessible from outside the class, you make it public. If you want a property to be accessible only from within the class, you make it private.
Here is an example of a private property in the Person
class:
class Person {
private var ssn: String = ""
}
fun main() {
val person = Person()
// person.ssn = "123-45-6789" // Error: Cannot access 'ssn': it is private in 'Person'
}
In this example, we define a private property called ssn
in the Person
class. The property is only accessible from
within the Person
class. If you try to access the ssn
property from outside the class, you will get a compilation
error.