+ 1
Can someone explain that get & set lesson.. in other way?
3 Réponses
+ 4
All right. So, when in your class you declare variables with the access modifier "private" these variables cannot be accessed from outside the class. That means that from outside the class their values can neither be accessed for use nor be changed.
That is when getters and setters come into play. We use getters to gain access to the variables and be able to use them from outside the class.
We use setters to change the values of the variables.
+ 5
As Yemibk mentioned, encapsulation is a means of properly securing your classes and its members from outside code. You do so by making the members of your class private and then setting up functions so that you can either set data to those members or get data from those members. Again, this make its much more secure and is good practice when dealing with classes/objects.
Code I posted earlier: (uses setters/getters)
https://code.sololearn.com/cvTpdrjVW2B6/#java
+ 1
Here... Take a look at this if you will. You can play around with it and create different objects of the class A to understand better how the setters and getters work.
https://code.sololearn.com/c9N4ew6OYwuU/?ref=app