encapsulation

>>> Do not be mislead into thinking that the access control system is to do with security. It is not designed to prevent a programmer getting at variables, it is to help avoid unwanted modification.



I was reading above lines from link
http://www.jchq.net/certkey/0601certkey.htm

I have not clearly understood it clearly especially  avoid unwanted modification. part.



Any ideas, resources,sample code,links,  highly appreciated. thanks in advance.
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
This can be continuation of my remark about separation of interface and implementation, see
http://www.experts-exchange.com/Programming/Languages/Java/Q_26948956.html?cid=1576 

Some of the variables in the class (actually usually the great majority of them)
 are relevant only to the implementation of the class; these variables are important
only for those programmers who are dealing with the implementation
of the class and its methods. Those programmers who are using the objects of this
class but are not developing the class itself usually do not need those fields
which are imporatnt only to implementation. It is considered to be a good practice in java
to declare those variables "private". If they are declared parivate, only those programmers who
write the code of this class - actually those who are deeply connected with
the internal fuinctioning of the class - will be able to view and modify those private parameters.
Other parameters of the class which are important to outside world can be public, but in fact
most frequently for generality they are also declared private, but the devleoper
of the class usually provides public methods, which expose values of those
externally relevant parameters, and also these methods may allow to modify
these paramters. This is all a good practice, usually your program will
work even if you declare your internal variables public, but in the environemnt
where you have separate people and groups of people working on one big project, it would be
easier for everyone if only those values, which are relevant to outside world
should be exposed for inspection and modification by the outside world.
In fact, such system provides a good service not only to developers of the
class (they can be guaranteed that no one from outside class
would be able to change some critical values) but also to outside
world, as such objects will show to outside world only those
features which are relevant to outside folks, in this way
it is much easier to study the objects, as your view is not cluttered
by the irrelevant details.


 
 

Sathish David Kumar NArchitectCommented:
>>>encapsulation

Hiding the data is know as encapsulation.

Thats why we are using access modifer like public,private,default

I think you know the use of this three...
SanthanaTechnical LeadCommented:
Wrapping of data and function into a single unit called class is called Encapsulation.
Usually in class we are declaring variables as Private and methods as  Public.

Because we are restricting other classes  to access the local  variable of that class .If we want to access that variable we used to give getter( ), setter() methods to access that variable.
The idea is we are just hiding the data from out side the class  and giving proper way to access the and change the data.
gudii9Author Commented:
>.In fact, such system provides a good service not only to developers of the
class (they can be guaranteed that no one from outside class
would be able to change some critical values)




outside people can still instantiate this class and call the private methods right. how we can protect variables in that case. please advise
for_yanCommented:
No, outside people cannot call private methods
But usually variables are private, but methods which are inrtended for outside use
are public. But it is still a protection, because with such rganization you can change only those
variables which are exposed to change in public method, and only through method.
And suppose you want tio impose tha variable acan have on,ly certain value - if it were public
outside fols can assign any value, but in method
you coudl frst have a check and throw error returning to the caller that thuis value iis unacceptable.
so, thsi way gives much better control.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.