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.

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

    ReplyDelete
  6. That was a great and informative blog.
    Java training in Nagpur

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

    ReplyDelete
  8. Nice Information. lear more information on our blog click here

    ReplyDelete
  9. Great blog, Thanks for sharing such information. https://vataliyacomputerclasses.wordpress.com/2024/10/14/java-programming-classes-in-vadodara-gujarat/, https://vataliya-soft-tech.blogspot.com/2024/09/java-programming-class-in-vadodara.html

    ReplyDelete
  10. Great article! The explanation about real-time project learning and industry-oriented Java training is very helpful for students and working professionals. Practical exposure is very important to understand how Java is used in real company environments.

    I found this Best Java Real Time Projects Online Training in 2026 program very useful for gaining hands-on experience and improving job readiness. It is a great opportunity for anyone who wants to work on real-world projects and strengthen their Java development skills.

    https://ashokitech.com/java-real-time-projects-online-training/

    ReplyDelete
  11. Great explanation of Core Java concepts and their importance. Learning Java along with AI integration helps developers stay ahead of the competition. I recommend checking Best AI powered Core JAVA Online Training in Hyderabad for structured learning.

    ReplyDelete
  12. I enjoyed the clear and straightforward approach used in this post.azure devops course

    ReplyDelete
  13. Great article! A MongoDB Full Course is a complete way to learn NoSQL databases, MongoDB architecture, CRUD operations, data modeling, and database management. The practical approach helps learners understand real-world applications.mongodb full course

    ReplyDelete