Kotlin Tutorial

Kotlin Classes

Constructor

A constructor is a special method of a class in OOP that initializes a newly created object of that type.
In Kotlin, There are two types of constructor – Primary and Secondary.

Primary Constructor:

Primary constructor is the part of the class header. It can be created by using () after the class name and optional type parameters (like regular parameters in functions).

Example:

				
					class Human(var name: String,var role: String)
				
			

Notice: The var keyword before each parameter. It means these are the properties of the class.

Let’s get things more interesting by creating the objects of the class

				
					val h1 = Human("Steve Jobs", "Founder")
val h2 = Human("Tim Cook", "CEO")
				
			

Done? Let’s print it now:

				
					println("I'm ${h1.name}; ${h1.role} of Apple")
println("I'm ${h2.name}; ${h2.role} of Apple")
				
			

The code above will outputs:

				
					I'm Steve Jobs; Founder of Apple
I'm Tim Cook; CEO of Apple

				
			

Secondary Constructor:

Kotlin classes can have one or more secondary constructors. They are created by the keyword constructor.

Example

				
					class Human {
   constructor()
}

				
			

Now the question is how to use it?

Unlike the primary constructor, you have to create a variable inside the class that will be the property of the class.

Example:

				
					class Human {
   var name = ""
   constructor()
}

				
			

Good, now add it inside the secondary constructor.

Example:

				
					class Human {
   var name = ""
   constructor(name: String) {
       // Body
   }
}

				
			

Notice: Unlike the primary constructor, we have a body {} for secondary constructor also.

One last thing remaining here, initialize the variable (property) inside the constructor body.

Example:

				
					class Human {

   var name = ""
   constructor(name: String) {
       // Body
       this.name = name
   }
}

				
			

This Means: The name declared inside the secondary constructor

				
					constructor(name: String)
				
			

initialized to the variable

				
					var name = ""
				
			

(property).

Create an object and access it.

Example:

				
					val h1 = Human("Steve Jobs");
println(h1.name)
				
			

RUN your code; above code will outputs:

				
					Steve Jobs
				
			

Want to know? There is one more situation 💡.

What if we have an property in primary constructor age: Int: You will see a compile time error (primary constructor call expected) below the secondary constructor right?.

				
					class Human(var age: Int) {

   var name = ""
   constructor(name: String) {
       // Body
       this.name = name
   }
}

				
			

In this case you have to put the same property inside the secondary constructor, modify your constructor accordingly:

				
					constructor(name: String, age: Int)
				
			

Done? But still the same compile time error; OK now with the help of this keyword call the primary constructor and pass the property inside that will simply instantiate the field variable (property) of age:

				
					constructor(name: String, age: Int) : this(age)
				
			

With this done, If you have the object already created then you will see the compile time error again below the object otherwise create the object:

Change the object from this :

				
					val h1 = Human("Steve Jobs");
				
			

To

				
					val h1 = Human("Steve Jobs", 23);
				
			

Or whatever age you want to put 😁.

One more thing:

				
					println(h1.age)
				
			

The code above outputs:

				
					Steve Jobs
23

				
			

Solution Code:

				
					fun main(args: Array<String>) {

   val h1 = Human("Steve Jobs", 23);

   println(h1.name)
   println(h1.age)

}

class Human(var age: Int) {

   var name = ""
   constructor(name: String, age: Int) : this(age) {
       // Body
       this.name = name
   }
}