‫Welcome back.
‫In this video we'll learn about getters and setters.
‫In Kotlin, you can use getters and setters to control access to the properties of a class.
‫Getters are used to retrieve the value of a property, and setters are used to modify the value of a
‫property.
‫By using getters and setters, you can encapsulate the internal state of a class and apply custom logic
‫when reading or writing a property.
‫When you define a new variable in a class, there are hidden getters and setters inside this class,
‫and it's equivalent to this writing.
‫We have a hidden get equals to the field.
‫This is the getter, and a set method takes a value and this field equals to the value.
‫This is the setter and this is the getter.
‫But you don't need to write this when you don't have any custom getters and setters.
‫You need to apply when getting and receiving or setting and modifying the value of a property.
‫But in this video, I'll show you how to create custom getters for your values.
‫I'll take the model year and when using the getters and setters, you need to initialize it directly
‫without using the init.
‫And here I'll create a custom getter get method.
‫And whenever I need to get the model year of robot, I need to display a message telling the user that
‫getting the model year.
‫Please wait.
‫Okay, this is our custom getter.
‫Then I need to return the field.
‫If we go to hello linked file, I need to get the firing robot to dot model year.
‫This is how we use the getters.
‫I used it println so let me run.
‫Here I am getting the model year from the fighting robot.
‫You can see that there is a new message here.
‫Getting the model year.
‫Please wait and then it will return for us.
‫The model year for this fighting robot two.
‫This is a custom getter.
‫Now let's move to create a custom setter.
‫I'll start with set.
‫And here I need to pass the value.
‫And whenever we need to set a custom value for this model year, I need to create a println message
‫telling the user.
‫Changing the model year.
‫Please wait.
‫Then I can create conditions here.
‫If the value which is a string equals to.
‫For example 2025 print ln.
‫You can't create future robot, else if it is not equal to 2025, it will set the field equals to the
‫past value.
‫Let me go back to hello Kotlin.
‫And here I'm using the custom setter firing robot two dot model year and make it 2025.
‫And here we go.
‫Changing the model year.
‫Please wait.
‫You can't create future robots.
‫But if I change the model year to 2020 run and it's changing the model year, and if I try to call my
‫custom getter.
‫Ron and I am changing the model year.
‫I'm getting the model year and the new model year is set at 2020.
‫Okay, this is how we use custom getters and custom setters in Kotlin.