Wednesday 3 September 2014

Concept of Encapsulation in Java



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.

16 comments :

  1. 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

    ReplyDelete
  2. Thank u for Sharing a good and useful information. Keep Sharing These type of articles

    Hadoop Training in Hyderabad

    Java Training in Hyderabad

    ReplyDelete
  3. 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.

    python Training in Bangalore | python Training in Bangalore

    ReplyDelete
  4. 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.
    Java Training in Bangalore

    ReplyDelete
  5. 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
  6. This comment has been removed by the author.

    ReplyDelete
  7. informative blog , keep posting and dont forget to checkout our blog java classes in satara

    ReplyDelete