Concept of Encapsulation in Java :
Encapsulation is one of the Object Oriented
Programming fundamental means wrapping
(encapsulating) up of data or fields
in a single method, make each field private (access specifier) and give the
access of method to users (by making the method public) and in this way we can
save each field from outside world.
If we provide each single data to outside world
than anyone can change its values which will result in breaching of security.
Encapsulation is sometimes called Data Hiding that means we hide single
field and provide the method to user for accessing that data or field.
To understand encapsulation see the diagram shown
below :
Why we need Encapsulation :
Encapsulation is needed to restrict the access of
raw data to outside world. To understand this statement, let’s take an example
:
Suppose I have a bank account which is maintained on
a software system and if I want to do a transaction than I can directly access
all the variables relating to the account.
Let’s say there is a field, myBalance of integer
type which is public. Now I can change or access its value by the object of
Account class.
Now, consider the case when I withdraw some cash
from my account, say 500 than I have to change the value of myBalance by
decreasing the amount by 500 by this statement :
obj.myBalance = myBalance – 500;
// where obj is an object of type Account.
If I can directly access this variable than rather
subtracting this amount, I can add double of that amount to myBalance or even
add any balance that I want by the use of below statement :
obj.myBalance = myBalance + 1000;
That means I can freely change my balance which
results in big loss to banks and here the concept of Encapsulation comes into
the picture.
Through the use of encapsulation we can provide debit
method to user and credit method to user for withdrawing cash and crediting
cash respectively in account and also make the variable myBalance private.
For e.g.
private int myBalance;
public int debitCash(int cash)
{
myBalance = myBalance – cash;
return myBalance;
}
public int creditCash(int cash)
{
myBalance = myBalance + cash;
return myBalance;
}
I hope you guys are clear that why we use
encapsulation.
Very Useful Post!Thanks!
ReplyDeleteJava Programming Help - Encapsulation
Encapsulation is a part of the object oriented concept and java runs on the oops concept so encapsulation has to be there for sure. Thanks
ReplyDeleteThank u for Sharing a good and useful information. Keep Sharing These type of articles
ReplyDeleteHadoop Training in Hyderabad
Java Training in Hyderabad
This is the best article for java encapsulation .
ReplyDeletethanks
Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
ReplyDeletepython Training in Bangalore | python Training in Bangalore
nice post.thank you
ReplyDeletejavascodegeeks.net
I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
ReplyDeleteJava Training in Bangalore
Dear Here Your Article nice
ReplyDeleteLearn Programming Languages
Nice. Also check java training
ReplyDeletejava online training
ReplyDeleteThis post is so interactive and informative.keep update more information...
Java Training in Bangalore
Java Classes in Pune
Java Training in Hyderabad
Java Training in Delhi
Java Training in Gurgaon
Struggling with your academic assignment? Struggling with programming Assignment ? No worries! sampleassignment.com is finally here for you. With us, you'll get the best support for your academic problems. We've got a team of experts ready to provide you with the best Programming Assignment help. All you have to do is ask and our experts will provide it right away! Along with that, we also offer huge offers and discounts in online assignment
ReplyDelete
ReplyDeleteJava Training in Noida
This comment has been removed by the author.
ReplyDeleteThat was a great and informative blog.
ReplyDeleteJava training in Nagpur
informative blog , keep posting and dont forget to checkout our blog java classes in satara
ReplyDeleteNice information .Thanks for sharing
ReplyDeleteIT Training and Placement
Java Programming Course