+ 2
What is the need of getter and setter method?
9 Antworten
+ 3
Get functions to access private data and Set to assign values to it.
+ 2
You are only limiting direct access to variables. For example a setter can be private but does not have to. You can use your private setter only in your constructor for checking a value for specific conditions. Another kind of use is to make the setter public. Even if you think that this does make no sense, it does. Sure you are right that this is a bit weired but you just have to think of the manipulation process. By using a setter function its way more controllable how your value can be changed.
+ 1
it is need to prevent from driect to access Because of that, setter is private,( because a person can change the value), but getter is public, because everyone can get the value
+ 1
Andreas...is my example wrong???
0
The concept getter and setter are needed for is called encapsulation. It is part of object-orientated programming. You need them to limit access to private attributes of classes.
0
I know.but I could not understand how I m limiting. methods are public
0
should I say...it is like... only bank will deposit money and we can access from any ATM..
if wrong..give me some practical example
0
Here is an Example. There is a post box. This post box is public. Someone throws in a message (setter). This post box is not locked, so everyone can open it to see what the message is. The point is that you cannot access the message on a direct way. You have to know where your post box is, to throw something in or get something.
0
your example is ok but misses the setter function as I see.