What are other purposes of private method/variable other than for protection.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Encapsulation
To hide the internal workings of an object so that it’s main purpose and functionality are clearer, and easier to manipulate.
To Force Access through Accessor/Mutator Methods Only
A variable may be declared private, forcing programmers to use the accessor or mutator methods instead. These methods may perform calculations, other checks and balances, you name it. Basically, it prevents any classes from modifying this variable in a stand-alone kind of way without using it’s accessor or mutator.
Deny Access In-General
As you suggested, a variable can be made private for protection, to prevent outside classes from tampering with the variable all together.