In this tutorial you will learn about the Kotlin Constructor and its application with practical example.
Kotlin Constructor
A class in Kotlin can have a primary constructor(The header of class) and one or more secondary constructors. The primary constructor goes after the class name. The constructor is way to initialize class properties.
Kotlin Primary Constructor
Syntax :-
1 2 3 |
class ConstructorDemo(val tutorial: String, var rating: Int) { // class body } |
Here, block of code which is surrounded by parentheses is represents a primary constructor. It have two properties tutorial and rating (val article: String, var review: Int)
Example: Let’s get understand it by an example.
1 2 3 4 5 6 7 8 9 10 11 |
fun main(args: Array<String>) { val constructorDemo = ConstructorDemo("W3adda.com/Kotlin", 9) println("Tutorial Name = ${constructorDemo.tutorial}") println("Rating = ${constructorDemo.rating}") } class ConstructorDemo(val tutorial: String, var rating: Int) { } |
Output :- sdf
Primary Constructor with Initializer Blocks
The primary constructor can not contain code, it has constrained syntax. For initialization code we used initializer block with init keyword. Check below example for know more.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
fun main(args: Array<String>) { val constuctorDemo = ConstructorDemo("W3adda/Kotlin", 9) } class ConstructorDemo(tutorialName: String, Rating: Int) { val tutName: String var Rat1: Int // Initializer block init { tutName = tutorialName.capitalize() Rat1 = Rating println("Tutorial Name = $tutName") println("Rating = $Rat1") } } |
Output :- sdfsd
Kotlin Secondary Constructor
A class can contain one or more secondary constructor in Kotlin using constructor keyword. It is required when you required more than one constructor in Kotlin class.
When you need to extend a class which provides multiple constructors that initialize the class in different ways , the Secondary Constructor is used for this.
Creation of Secondary Constructor: Let’s see how you can create a secondary constructor in Kotlin.
1 2 3 4 5 6 7 8 |
class SecondaryConstructorDemo1 { constructor(data: String) { // some code } constructor(data: String, numberOfData: Int) { // some code } } |
Here, you can see in above code that SecondaryConstructorDemo1 is not has primary constructor.
Example: Let’s understand it by an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
fun main(args: Array<String>) { val ps1 = SecondaryConstructorDemo1("Wrong Password") } open class SecondaryConstructorDemo { var data: String = "" var numberOfData = 0 constructor(_data: String) { } constructor(_data: String, _numberOfData: Int) { data = _data numberOfData = _numberOfData println("$data: $numberOfData times") } } class SecondaryConstructorDemo1 : SecondaryConstructorDemo { constructor(_data: String) : this("W3Adda : From SecondaryConstructorDemo1 -> " + _data, 5) { } constructor(_data: String, _numberOfData: Int) : super(_data, _numberOfData) { } } |
Output: The below image will be output of this code.