Getter/Setters in kotlin- Data Security

Satheesh Guduri
2 min readSep 8, 2021
setters

We know that, class is a model/blueprint to create the objects in other hand we can also say class is a user defined datatype. let’s create the class Student

class

In, kotlin creating the class is very simple, Here Student(var id:Int, var name:String) is primary constructor and (id,name) are class variables. But, here any one can access the data of the Student class. So, how can we provide the security for data, just we use getters/setters.

security

In the above code, var name:String = “ “, the very next line we have to write the set(value) method, if you want to change the student marks then the password is satheesh- so here we are checking for the password, if password matches then we can give the chance to modify the marks of students in this way we can provide the security. And also for getting the data we can provide the security.

Here set(value), the value is nothing but the data which is coming from out side and we can se the data like field = value.

So, the last point is after declaration the any variable we have to add set and get methods.

Thanks for reading this article, if you like my article please clap for me.

--

--