Encapsulation in Java with Real World Example

Price: INR 1,299.00
Was: INR 1,799.00

Encapsulation is the process of wrapping the data and functions together or hiding the data from the outer world.

It is the idea to hide the implementation details from the user and avoid the direct access to the class members from the outer world.

We can achieve the encapsulation in our class, by making the entire data member as private and create getter, setter method to access that data member.

 
 

Encapsulation with Example

Encapsulation is all about protecting anything which is prone to change. Encapsulation makes the code maintenance easy because in encapsulation we bundle the similar code in one place. So it’s easy to maintain and change the code in future. It will also help you in a way that your class members will not be accessed by the outer world directly so anybody can’t destroy your code by just extends your class.

This can be better explained with a simple example of encapsulation in Java. Suppose we have a class “Loan” class and data variables Loan, borrowerAge, and Amount. So you can protect your code by using encapsulation. Suppose you are not using a concept of encapsulation then you will declare your data types public. Now anyone can access your data members.

Some other developer extends your class and sets your borrowerAge value 200 (Not intentionally) now it’s not feasible in real life scenarios. So you need to use the concept of encapsulation to protect your data members. Now to implement the concept of encapsulation you need to declare your class data members private and use setter and getter functions to access these value.

So the outer world can access your code and change your data members value using your setter function, which will revoke him/her to assign the invalid value to your data member.

Output :

Loan is – HDFC_Home_Loan
Borrower Age is 45
Loan Amount is $50000

 

In above class, we have made all the data members private and declare the setter and getter method to set and get the data members value. You noticed we have put the check on borrowerAge. Now if anyone tries to provide the invalid value then it will set the borrower age to 0 and log the error message as well. So using encapsulation we can hide and protect our class members.

 

Advantage of Encapsulation:

  • Encapsulation improves code maintainability and flexibility and re-usability.
  • Encapsulation allows you to change one part of code without affecting another part of the code.
  • Encapsulation in Java allows you to control who can access what.
  • By providing only getter and setter method access, you can make the class read-only.
  • Encapsulation allows you to change one part of code without affecting another part of the code.
  • A class can have total control over what is stored in its fields. Suppose you want to set the value of marks field i.e. marks should be positive value than you can write the logic of positive value in setter method.
  • Encapsulation helps in loose coupling and high cohesion of code.

 

Selenium Testing Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

 

1 Comment
  1. Mark Alex 7:23 PM / July 14, 2017 - Reply

    Appreciation to my father who informed me regarding this blog, this blog is genuinely remarkable.

Leave a Reply