Encapsulation is a idea in Object Oriented Programming (OOP). It is nothing new and it is as well-known as John Knitter guides among children. It has been written a guide on connections, like organization, gathering or amassing, structure, abstraction, etc and it was well obtained. I have been preparing for too much time to create about all the OOPsideas and this guide on encapsulation is one among that sequence.

java enc

Why is it named Java encapsulation? Do we have a great gift for encapsulation in java? Let us be obvious, encapsulation is a OOP idea and we will see an example of how it is applied in Java. The key ideas in OOP are abstraction, encapsulation, bequest and polymorphismand we will have a individual guide for each of these OOP ideas.

Encapsulation Definition

Encapsulation is to be able to program information, relevant actions in a product program and control/restrict accessibility them (both information and function) from other things. It is all about appearance relevant things together and cover up them from exterior components.

We can see that search phrases encapsulation information concealing are used synonymously everywhere. It should not be misinterpreted that encapsulation is all about information concealing only. When we say encapsulation, focus should be on collection or appearance or combining relevant information and actions together.

Encapsulation is nothing but defending anything which is vulnerable to modify. logical behind encapsulation is that if any performance which is well exemplified in rule i.e managed in just one place and not spread around rule is not hard to modify. This can be better described with a easy example of encapsulation in Java. We all know that constructor is used to developing item in Java and constructor can agree to a disagreement. Assume we have a class Loan has a constructor and then in various sessions, you are creating an type of the financial loan by using this constructor. now specifications modify and you need to add the age of client as well while getting credit.

When we style a class in OOP, the first concept we should have in thoughts is encapsulation. Team the attached information and its actions in a pail. Main advantage of encapsulation is, better maintainability.

That’s it! This is all about encapsulation. Let us keep it as easy as that. Nothing more and nothing less.

Encapsulation in Java

Any well described Java class in its perspective sector is an example for encapsulation in Java. So the significance here is for combining information and techniques together only after that information concealing comes into image. When we say information concealing, we all know about accessibility specifiers in Java. Hiding an feature or technique is simpler from the exterior globe. Just use the accessibility specifier ‘private’.

So information concealing is pretty easy. How about combining information and technique. This is very important. We should comprehend well about the company sector and depending on that style the class and group features and its techniques together. This technique is key in encapsulation.

Following is a summary example for Java encapsulation. When we discuss an creature, we should have all its features detailed, in the same way its actions like how it will search, run, partner, etc. By combining all these information and actions in only one class we are following encapsulation concepts.


public class Animal {
private String animalName;
private String animalType;
private int height;
private String color;

public Animal(String animalName, String animalType) {
this.animalName = animalName;
this.animalType = animalType;

public void hunt() {
// implementation of hunt

public void run() {
// implementation of run

public void mate() {
// implementation of mate
//encapsulation is not about having getter/setters
public String getAnimalName() {

return animalName;

public void setAnimalName(String animalName) {
this.animalName = animalName;

public String getAnimalType() {
return animalType;

public void setAnimalType(String animalType) {
this.animalType = animalType;

There is a range of distinction between abstraction and encapsulation, which you can comprehend with a easy example. We will see about that in next guide on abstraction. Exercise encapsulation while you style the classes.

You can the institute of Java developer or the best Java classes in Pune to make your career in this field.

Check our JAVA REVIEWS here.