Kotlin Constructors & Init Block
Every object has 2 things - Properties and Methods. To initialize properties with the default values, constructors are used. Once the object is created, properties are initialized using the constructor. Let's understand this with an example - fun main() { var car = Automobile("Car", 4, 4) var auto = Automobile("Auto", 3, 3) } class Automobile(val name: String, val tyres: Int, val maxSeating: Int) { fun drive() {} fun applyBrakes() {} } Explanation - Here we have defined a class named Automobile. It has 3 properties - name, tyres, and max seating. This portion of the class definition (val name: String, val tyres: Int, val maxSeating: Int) is known as Constructor. In the main method, we have created 2 instances of this Automobile class and passed the required values for properties. For car instance, we have passed, name as Car, tyres and max seating as 4. Values we have passed are used to initialize the properties ...