This course covers the concept of Object-Oriented Programming in Kotlin, which is a method of designing and implementing software. It simplifies software development and maintenance by providing concepts such as object, class, inheritance, polymorphism, abstraction, and encapsulation. This course will explore those.
Intended Audience
This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.
Prerequisites
This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.
Resources
Course GitHub repo: https://github.com/OakAcademy/Kotlin-Programming-Course/tree/main/ObjectOrientedProgramming
Well, hello my friends. So, we've already learned the primary and secondary constructors. And we also saw that we can change the properties of the object that we create from a class later on. But sometimes in the projects that we develop after assigning value to an object, we may want the assigned value not to be changed. So, in these such cases we should use the method called Encapsulation. So, why don't we just dig right in without any further ado?
Open up Android Studio and let's get into this encapsulation process. I'm sure you remember after we determine the name and the model of the object named mySecondCar back in the previous video, we were able to change it again. So, what should we do if we don't want these properties changed again, later on, after we've already determined the first property of the object named mySecondCar using constructor? Well, that's right. I think I heard a couple of you say, "Yeah, what about the visibility modifier?" So indeed, let's use the private visibility modifier. So, for those of you who haven't read that far ahead, why don't we go ahead and learn how to do it? So, I want to open up the class called MySecondCars. Now in this class, we have defined two variables named: name and model. Let's make the model variable private here. So, I'll just type in 'private.' Now the model variable has a private visibility modifier. So, in other words the variable named model can only be accessed within the MySecondCars class. So, to better understand it, let's reopen that Kotlin file. And what do you see? We cannot change the model of the object named mySecondCar, because model variable is now private and can only be accessed from class it is in. Cannot be accessed from anywhere else. All right so, if you don't want an object's properties to be changed to a second time, you really should use the private visibility modifier. That's the process called encapsulation.
Of course, it is worth mentioning one more thing here. I want you to notice that after making the model variable private, we cannot retransfer the value. We also wouldn't be able to read the model variable. So, what should we do if we want to read the value of the model variable? Well, at this stage we'll need to set and get. In fact, the operation that we do here, that is the value transfer operation, is the set operation. So, the operation that we do here, that is the operation to read the value that we transfer is the get operation. All right, so we can block the set operation. And what do you think? Can we block the set operation and allow the get operation? Well, why don't we have a look? So, I'm reopening MySecondCars class. First of all, we'll delete the private statement here. Okay, so now let's have a look at this subject through the model variable again.
So, from the line where we define the model variable, we go to the bottom line by pressing 'Enter', then press a Tab key, and I'll write 'private set.' So, this process only makes the set making process private. So, we can no longer pass a new value to the model variable, but we can read the value of the model variable. You can even write get on the bottom line. So, now let's go back to the Kotlin file and as you can see, we cannot change the value of the variable named model, but we can read the value passed to this variable. That's because the set operation is private. What do you think? I think that's really cool. So, thank you my friends. That's how you use encapsulation, set and get in Kotlin programming. So, why don't we take a short break?
Because next time we're going to learn about inheritance. So, I'll see you in the next video.
Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.