Sure, here is the tutorial:
In this tutorial, we'll delve into the world of constructors and initializers in Kotlin. We'll learn how to define constructors, how to initialize objects, and understand their importance in object-oriented programming.
Goals of this tutorial:
What you'll learn:
Prerequisites:
In Kotlin, a constructor is a special member function that is used to initialize the properties of a class. There are two types of constructors in Kotlin: primary and secondary.
The primary constructor is part of the class header. It goes after the class name.
A secondary constructor is defined inside the class body. It starts with the constructor keyword.
An initializer block is part of a class body. It's prefixed by the init keyword. It's executed when the class is instantiated, allowing you to initialize the class properties.
Example 1: Primary Constructor
class Person(name: String, age: Int) { // primary constructor
// properties
var name: String = name
var age: Int = age
// initializer block
init {
println("Person is initialized with parameters: Name = $name, Age = $age")
}
}
fun main() {
val person1 = Person("John", 20) // creates Person object
}
Explanations:
class Person(name: String, age: Int)
: This is the primary constructor.var name: String = name
and var age: Int = age
: These are properties initialized with constructor parameters.init
block is used for initialization.Expected output:
Person is initialized with parameters: Name = John, Age = 20
Example 2: Secondary Constructor
class Person {
var name: String
var age: Int
// secondary constructor
constructor(name: String, age: Int) {
this.name = name
this.age = age
println("Person is initialized with parameters: Name = $name, Age = $age")
}
}
fun main() {
val person2 = Person("Jane", 25) // creates Person object
}
Explanations:
constructor(name: String, age: Int)
: This is the secondary constructor.this.name = name
and this.age = age
: This is how we initialize properties with constructor parameters in a secondary constructor.Expected output:
Person is initialized with parameters: Name = Jane, Age = 25
In this tutorial, we learned about primary and secondary constructors in Kotlin, as well as initializer blocks. We understood how they are used to initialize objects and properties.
Next steps for learning:
Additional resources:
Exercise 1: Create a class Student
with primary constructor having properties rollNo
and name
.
Exercise 2: Modify the Student
class to have a secondary constructor which also includes age
property.
Solution and Explanation:
Exercise 1 Solution:
class Student(rollNo: Int, name: String) {
var rollNo: Int = rollNo
var name: String = name
init {
println("Student is initialized with parameters: Roll No = $rollNo, Name = $name")
}
}
Exercise 2 Solution:
class Student {
var rollNo: Int
var name: String
var age: Int
constructor(rollNo: Int, name: String, age: Int) {
this.rollNo = rollNo
this.name = name
this.age = age
println("Student is initialized with parameters: Roll No = $rollNo, Name = $name, Age = $age")
}
}
In these exercises, you have practiced creating classes with primary and secondary constructors. Keep practicing to become more proficient.