What is the need of getter and setter method?
Get functions to access private data and Set to assign values to it.
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.
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
Andreas...is my example wrong???
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.
I know.but I could not understand how I m limiting. methods are public
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
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.
your example is ok but misses the setter function as I see.