Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7
The getters and setters are methods of class It is use to get a value and set a value The real life example of this when our application is connected which database we use form to enter a specific record this can done by setter When we want to find value or specific person the getter can perform this functionality Like Passport Office When you want to make new passport they enter or set data Someone from government department want some information they get information I hope this answer can be clear your Concept ☺
20th Aug 2019, 3:32 PM
Muhammad Nabeel
Muhammad Nabeel - avatar
+ 4
It becomes insecure any one cange acces easily and change value
20th Aug 2019, 5:46 PM
Muhammad Nabeel
Muhammad Nabeel - avatar
+ 3
One of the main points of a class is that you have a protective case around the data of each instance. (Let me do it in Python.) class ProtectedInt: def __init__(self, n): self._n = n def set_n(self, new_n) if type(new_n) is int: self._n = new_n x = ProtectedInt(5) x.set_n(7.0) self._n is 'pseudo private', in other languages it would be genuinely private. If we changed that value directly, like... x._n = 'Bryan' ... then it would be not protected at all. We have just stored a string in an instance that should contain an int! By using the setter, the value is only changed if the argument is an int. Like this, you have protected the state of that object. This is a simple example demonstrating what this is about: You build the class in a way that each instance protects its own data and strictly controls how it can be accessed or modified.
20th Aug 2019, 3:50 PM
HonFu
HonFu - avatar
+ 3
The drawback is that you can freely access the data whichever way you want, so you could also store a value that doesn't make sense in the context of that type (like in my example above). The advantage of using a class almost disappear - you could just store your data in an array and it would be equally vulnerable.
20th Aug 2019, 5:48 PM
HonFu
HonFu - avatar
+ 3
The concept of information hiding and acces via getters/setters can become important in large projects like banking applications etc. where multiple programmers maintain the software and not just the original author/s.
21st Aug 2019, 11:46 AM
Sonic
Sonic - avatar