What is Encapsulation in Java and OOP with Example
What is Encapsulation in Java
Encapsulation is nothing but protecting anything which is prone to change. the rationale behind encapsulation is that if any functionality is well encapsulated in code i.e maintained in just one place and not scattered around code is easy to change.
This can be better explained with a simple example of encapsulation in Java. we all know that constructor is used to creating an object in Java and constructors can accept an argument.
Suppose we have a class Loan that has a constructor and then in various classes, you have created an instance of the loan by using this constructor. now requirements change and you need to include the age of the borrower as well while taking a loan.
Since this code is not well encapsulated i.e. not confined in one place you need to change everywhere you are calling this constructor i.e. for one change you need to modify several files instead of just one file which is more error-prone and tedious, though it can be done with refactoring feature of advanced IDE wouldn’t it be better if you only need to make a change at one place?
Yes, that is possible if we encapsulate Loan creation logic in one method say createLoan() and client code calling this method, and this method internally creates Loan object. in this case, you only need to modify this method instead of all client codes.
Example of Encapsulation in Java
class Loan{
private int duration; //private variables examples of encapsulation
private String loan;
private String borrower;
private String salary;
//public constructor can break encapsulation instead use factory method
private Loan(int duration, String loan, String borrower, String salary){
this.duration = duration;
this.loan = loan;
this.borrower = borrower;
this.salary = salary;
}
//no argument constructor omitted here
// create loan can encapsulate loan creation logic
public Loan createLoan(String loanType){
//processing based on loan type and then returning loan object
return loan;
}
}
In this same example of Encapsulation in Java, you see all member variables are made private so they are well encapsulated you can only change or access this variable directly inside this class.
If you want to allow the outside world to access these variables is better to create a getter and setters like the getLoan() that allows you to do any kind of validation, security check before return a loan so it gives you complete control of whatever you want to do and a single channel of access for the client which is controlled and managed.
Advantage of Encapsulation in Java and OOP
Here are few advantages of using Encapsulation while writing code in Java or any Object-oriented programming language:
- Encapsulated Code is more flexible and easy to change with new requirements.
- Encapsulation in Java makes unit testing easy.
- Encapsulation in Java allows you to control who can access what.
- Encapsulation also helps to write immutable classes in Java which is a good choice in multi-threading environment.
- Encapsulation reduces the coupling of modules and increases cohesion inside a module because all pieces of one thing are encapsulated in one place.
- Encapsulation allows you to change one part of code without affecting other parts of code.
What should you encapsulate in code?
Anything which can be change and more likely to change in the near future is a candidate of Encapsulation. This also helps to write a more specific and cohesive code. An example of this is object creation code, code that can be improved in the future like sorting and searching logic.
Design Pattern based on Encapsulation in Java
Many design pattern in Java uses the encapsulation concept, one of them is Factory pattern which is used to create objects. A factory pattern is a better choice than a new operator for creating an object of those classes whose creation logic can vary and also for creating different implementations of the same interface.
The BorderFactory class of JDK is a good example of encapsulation in Java which creates different types of Border and encapsulates the creation logic of Border. Singleton pattern in Java also encapsulates how you create an instance by providing a getInstance() method.
Since an object is created inside one class and not from any other place in code you can easily change how you create an object without affect another part of the code.
Important points about encapsulation in Java.
- “Whatever changes encapsulate it” is a famous design principle.
- Encapsulation helps in loose coupling and high cohesion of code.
- Encapsulation in Java is achieved using access modifiers private, protected, and public.
- Factory pattern, Singleton pattern in Java makes good use of Encapsulation.