why we use private keyword?i know it limits the access to a var or a method,but why we have to limit the access?
-
2read about encapsulation and its benefits. – setzamora Aug 08 '12 at 02:52
-
Think of it in terms of information you want to make available to other parts of the program. Let me co-relate it to an example which is the security clearance in an Defence forces. You want to make information available to only that person who has the required security clearance. You don't want to let everybody know all of the information until and unless he deserves to know. Same is with the programs.. you have a lot of classes with variables and methods. The private tells that this information should be made available to only this class as no other class needs this information. – user1420750 Aug 08 '12 at 02:53
5 Answers
If you expose all of the methods and variables publicly, when another programmer tries to do something with a class you have written it will be really hard because he wont know which methods take care of the internal behavior of your class, which are the methods he is not supposed to use because he would mess up the internal state of the objects and cause a bug.

- 36,476
- 32
- 115
- 163
You don't "have to" do anything of the sort. It's just good practice to only expose that which absolutely must be exposed, especially when you're creating a large program where connections increase exponentially, and risk of side effects increase with it. It's really all about managing complexity. Read up on encapsulation and information hiding and how this helps reduce complexity in large systems. A good book is Grady Booch's Object-Oriented Design for more on this.

- 283,665
- 25
- 256
- 373
In OO world to achieve encapsulation it is very essential to understand the functionality or behavior of object you would like to expose and each access identifier plays important role in it.In Code Complete book McConnell strongly encourages making all variables private.

- 627
- 6
- 9
Consider you are building a utility library and you are going to expose methods to external world as utilities. But those methods may call few methods which are private. So calling those private methods directly may not make any sense and in worst case, they may even harm the state of the object too. There are lots of examples to explain the usage of private even in jdk.
In String
class, there is a private method checkBounds
which just checks for valid constructor arguments in case of public String(byte bytes[], int offset, int length, String charsetName)
Exposing this method (making it public) makes no sense for String class.

- 1,760
- 12
- 28
private as the name implies it is something which resides private/unaccessable from the outer class. Object Oriented Programming language has one important concept called Encapsulation which means to restrict the access to some of the object's components. While developing a code you need to hide some objects from the other class, in these case delete those object/variable as private. A private access is only to the class where is it defined.

- 29,392
- 25
- 90
- 143