Kotlin supports both functional and object-oriented programming.
Kotlin supports features such as higher-order functions, function types and lambdas which makes it a great choice for working in functional programming style. You will learn about these concept in later chapters. This article will focus on object-oriented style of programming in Kotlin.
Object-oriented Programming (OOP)
In object-oriented style of programming, you can divide a complex problem into smaller sets by creating objects.
These objects share two characteristics:
- state
- behavior
Let's take few examples:
- Lamp is an object
- It can be in on or off state.
- You can
turn on
andturn off
lamp (behavior).
- Bicycle is an object
- It has current gear, two wheels, number of gear etc. states.
- It has braking, accelerating, changing gears etc. behavior.
You will learn about detail features of an object-oriented programming like: data encapsulation, inheritance and polymorphism as we go on. This article will focus on the basics to keep things simple.
Recommended reading: What is an object?
Kotlin Class
Before you create objects in Kotlin, you need to define a class.
A class is a blueprint for the object.
We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object.
Since, many houses can be made from the same description, we can create many objects from a class.
How to define a class in Kotlin?
To define a class in Kotlin, class
keyword is used:
class ClassName { // property // member function ... .. ... }
Here's an example:
class Lamp { // property (data member) private var isOn: Boolean = false // member function fun turnOn() { isOn = true } // member function fun turnOff() { isOn = false } }
Here, we defined a class named Lamp.
The class has one property isOn (defined in same way as variable), and two member functions turnOn()
and turnOff()
.
Recommended Reading: Kotlin functions
In Kotlin, either the property must be initialized or must be declared abstract
(Visit: Kotlin Abstract Class to learn more). In the above example, isOn property is initialized to false
.
Classes, objects, properties, member function etc. can have visibility modifiers. For example, the isOn property is private. This means, the isOn property can be changed from only inside the Lamp class.
Other visibility modifiers are:
private
- visible (can be accessed) from inside the class only.public
- visible everywhere.protected
- visible to the class and its subclass.internal
- any client inside the module can access them.
You will learn about protected
and internal
modifiers later in Kotlin visibility modifiers article.
If you do not specify the visibility modifier, it will be public
by default.
In the above program, turnOn()
and turnOff()
member functions are public
whereas, isOn property is private.
Kotlin Objects
When class is defined, only the specification for the object is defined; no memory or storage is allocated.
To access members defined within the class, you need to create objects. Let's create objects of Lamp class.
class Lamp {
// property (data member)
private var isOn: Boolean = false
// member function
fun turnOn() {
isOn = true
}
// member function
fun turnOff() {
isOn = false
}
}
fun main(args: Array<String>) {
val l1 = Lamp() // create l1 object of Lamp class
val l2 = Lamp() // create l2 object of Lamp class
}
This program creates two objects l1 and l2 of class Lamp. The isOn property for both lamps l1 and l2 will be false
.
How to access members?
You can access properties and member functions of a class by using .
notation. For example,
l1.turnOn()
This statement calls turnOn() function for l1 object.
Let's take another example:
l2.isOn = true
Here, we tried to assign true
to isOn property of l2 object. Note that, isOn property is private
, and if you try to access isOn from outside the class, an exception is thrown.
Example: Kotlin Class and Object
class Lamp {
// property (data member)
private var isOn: Boolean = false
// member function
fun turnOn() {
isOn = true
}
// member function
fun turnOff() {
isOn = false
}
fun displayLightStatus(lamp: String) {
if (isOn == true)
println("$lamp lamp is on.")
else
println("$lamp lamp is off.")
}
}
fun main(args: Array<String>) {
val l1 = Lamp() // create l1 object of Lamp class
val l2 = Lamp() // create l2 object of Lamp class
l1.turnOn()
l2.turnOff()
l1.displayLightStatus("l1")
l2.displayLightStatus("l2")
}
When you run the program, the output will be:
l1 Lamp is on. l2 Lamp is off.
In the above program,
Lamp
class is created.- The class has a property isOn and three member functions
turnOn()
,turnOff()
anddisplayLightStatus()
. - Two objects l1 and l2 of Lamp class are created in the
main()
function. - Here,
turnOn()
function is called using l1 object:l1.turnOn()
. This method sets isOn instance variable of l1 object totrue
. - And,
turnOff()
function is called using l2 object:l1.turnOff()
. This method sets isOff instance variable of l2 object tofalse
. - Then,
displayLightStatus()
function is called for l1 and l2 objects which prints appropriate message depending on whether isOn property is true orfalse
.
Notice that, the isOn property is initialized to false
inside the class. When an object of the class is created, isOn property for the object is initialized to false
automatically. So, it's not necessary for l2 object to call turnOff()
to set isOn property to false
.
For example:
class Lamp {
// property (data member)
private var isOn: Boolean = false
// member function
fun turnOn() {
isOn = true
}
// member function
fun turnOff() {
isOn = false
}
fun displayLightStatus() {
if (isOn == true)
println("lamp is on.")
else
println("lamp is off.")
}
}
fun main(args: Array<String>) {
val lamp = Lamp()
lamp.displayLightStatus()
}
When you run the program, the output will be:
lamp is off.
This article is just an introduction to object-oriented programming in Kotlin. Check these chapters in sequence to learn more:
- Kotlin Constructors and Initializers
- Kotlin this Keyword
- Kotlin Nested Class