‫Welcome back in Kotlin.
‫The init block is a special initialization block that is used to execute code.
‫When an instance of a class is created, it's often used to perform additional setup or initialization
‫for properties in a class.
‫The init.
‫The block is executed after the primary constructor, and any secondary constructors, if they exist,
‫have been called.
‫For example, if I need to create a message on every creation of a new robots, here a new robot is
‫created.
‫Back to the Hello Kotlin file.
‫We have three instances.
‫If I run the program, we get three messages telling us that a new robot is created.
‫So in this case, every time you create a new robot, this code will be executed.
‫The init block can be helpful for performing any necessary setup tasks or computations when creating
‫class instances, it's especially useful when you need to ensure certain properties are correctly initialized
‫when an object is created.
‫Now let's move to the secondary constructors.
‫Secondary constructors in Kotlin are additional constructors that can you define in a class to provide
‫alternative ways to create instances of that class?
‫Unlike the primary constructor which is declared in a class header, secondary constructors are defined
‫using the constructor keyword within the class body.
‫Secondary constructors allow you to initialize class properties and perform custom initialization logic
‫when creating objects.
‫In this example, I'll add a new property here in the primary constructor for the model year.
‫So model year it's of type string.
‫Sometimes we need to create new robots with with a model year.
‫But some robots we don't have the model year.
‫We need to create a secondary constructor that allows us to have a single parameter, which is the name,
‫without passing the model year.
‫So to create instances of general robot and its sibling and children classes like firing robot, super
‫robot and ordinary robot, we need to create a secondary constructor having only name and another constructor
‫that has all the parameters.
‫Please pay attention.
‫I remove these from the primary constructor and here I'll create another constructor.
‫Here is the primary constructor.
‫So I need to specify name.
‫It's of type string and model year.
‫It's of type string.
‫Another constructor name it's of type string.
‫Now I can create objects having name and model year or objects having only name.
‫You can use primary and secondary constructor in the same class, but for that you have to authorize
‫the secondary constructor to the primary constructor, which is done with the help of this keyword.
‫This and here use this keyword.
‫And when using the constructor keywords and passing the parameters here, we can see that if we scroll
‫down to the speak method, we have an error because the name variable doesn't exist.
‫And this is a problem.
‫And the solution of it is to create the global variable here inside the class.
‫So these variables are called class variable name is of type string and var model year it's of type
‫string.
‫I need to initialize these variables.
‫So here this dot name I'm referring for this in the class variable equals to the name which is passed
‫as a parameter in the constructor of this class.
‫In the same way, this model year equals to the model year that it is passed from here.
‫Again, in the other constructor, this dot name equals to name.
‫I'm passing this.
‫Initializing this name equals to name that it is passed.
‫And this dot model year.
‫By the way guys there is no model year here passed in the constructor.
‫So its default value would be unknown model year okay.
‫So when you create an object of robots without specifying the model year, the unknown model year will
‫be its value.
‫Okay, it's very simple.
‫This is how we use and why we use secondary constructors.
‫Scroll down the speak method.
‫The name variable is solved because we created as a global variable.
‫And here we need to.
‫Initialize can be done like this.
‫Or we can use it inside the init block name equals to empty string and model year equals to empty string.
‫When using strings you need to initialize them.
‫Till now we can't create objects of filing robot, super robot or ordinary robot.
‫Without name and model year.
‫We need to be able to create ordinary robot, super robot and firing robot with names or names and model
‫year.
‫For that we need to modify these classes.
‫So here I will remove this parameter and I remove this constructor.
‫And I'll create a constructor keyword starting with a name.
‫It's of type string and model year is of type string.
‫And here I need to call the super keyword and pass the name and model year.
‫In the next video we'll learn about the super keyword.
‫Also, I need to create another constructor name.
‫It's of type string.
‫Now I am able to create instances of firing robot that can have name and model year or only name.
‫Let me go back to the Hello Kotlin file and here I'll create another instance.
‫Val.
‫Firing robot two equals to firing robot hero two.
‫And the model year is 2024.
‫You can see that the firing robot is accepting a model year parameter.
‫Let me show you robot two dot file.
‫Run the program and you can see that there is a new robot is created and it's firing.
‫Also, I can access the attributes of this robot control and space and we can get the model year.
‫So print ln the model year run again and it's 2024 okay guys it's very simple.
‫This is the usage of init and secondary constructors in Kotlin.