OOPS Concepts: Encapsulation Made Easy

One of the most important OOPS concepts is Encapsulation.

Encapsulation is a technique in java that helps us combine data and relevant methods together as a single unit. Java programmers make use of encapsulation to assemble their methods into a single unit; there will be multiple such units in any application.

Let’s take a few real world examples to get a clear idea of Encapsulation.

OOPS Concept of Encapsulation in the Phone Box


Accessories in a phone box

Encapsulation in a phone box

When buying a phone, all the accessories of the phone such as the charger, earphones, battery and warranty card are packed along with the phone.

When you buy that phone, these accessories come along with them. What if they sold all the accessories separately without a box?.

Manufacturers would find it tedious to send them to their offline dealers. In case of online sales, customers will have to buy each component separately and take them to a shop for assembly after receiving them. On top of that, each of the components may be ship separately.

Phone and its accessories are correlated electronic components. It’s much easier for everyone – from the manufacturer to the customer – to handle them together in a box.

Let’s take another example.

OOPS Concept of Encapsulation in Homes

Compartmentalization in houses

Encapsulation in homes

When architects build homes, they give a definite area for the rooms inside the house. They define a certain limit for different rooms: Walls to separate the rooms. Doors to enter and exit the rooms inside. Each room contains objects which must belong in that room.

For example, a sofa belongs to the living room. Similarly, a bed belongs to the bedroom. To differentiate the area of bedroom from living room, kitchen or bathroom, we have walls. Just imagine – how weird would it be if we kept everything in the house WITHOUT walls?

We would see across the house and everyone’s private businesses wouldn’t be private anymore. Light in the kitchen area may not let someone sleep in the bedroom. Noise will spread along the whole house.

We need to compartmentalize because every area has its own specific purpose and others should not see it.

Architects created the concept of rooms with the idea of grouping relevant furniture together and keep it separate from the rest of the furniture.

Because we are a creating a room and putting all the relevant and interrelated furniture together, we have achieved Encapsulation here.

Relation between the Two Encapsulation Examples

  1. Both the examples show how important it is to keep the related components, grouped together.
  2. They also show that the dependency between the components is high within themselves. They can interact with the outside components but in a limited way.
  3. It also makes the work of the phone dealer or architect, much easy by compartmentalized products and their contents together.

Encapsulation in Java

In both examples, we understand that, even though humans use encapsulation in almost every aspect of life, they fail to realize it. Encapsulation isn’t rocket science. It is a part of living. Java gives us an opportunity to make encapsulation a part of programming as well.

Encapsulation means grouping the data and methods relevant to a single entity into a class. A class is essentially the basic building block of not just Java, it’s the core of all of object-oriented programming. Any application in Java (be it stand-alone app, applets, servlets, or even massive Android apps), we start off by creating a class.

Even if you create functions that don’t belong to a class, you still have to write them inside a class.

And this brings us to a very important question.

Is Java a fully object-oriented programming language?

A simple and straight-forward answer to this is – NO. A programming language is said to be object-oriented if and only if it contains objects in each and every aspect of it. This means it should not have even a single feature which does not objects.

There are small bits in Java which do not rely on objects, we have learnt to use them without creating an object. These are:

  • Java has primitive data types, which are nothing but atomic units of data. Quite simply, they have just single values and nothing else inside them.


  • As we learnt before, a main method is the key to your application, it is the first function from which the execution of your application will start. Even though we said main method will reside in a class, Java does not need to create an object of its enclosing class to be able to run it; JVM calls the main method directly without any object creation.

Because of these reasons, we’ll say that Java is 99.9% object-oriented because does not have object orientation in each and every aspect of it.

Encapsulation is one of the four pillars of OOPS, other three being Abstraction, Inheritance, and Polymorphism. All these features can be quite confusing. Therefore, we have tried to cover each of these in best possible ways. Comments are the place where you can give us feedback about our website and content.

Leave a Reply