The Golden Rules of Encapsulation Concept of Encapsulation

image credit

As per wiki definition of Encapsulation here 

In programming languages, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:
A language mechanism for restricting access to some of the object's components.
A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.
Some programming language researchers and academics use the first meaning alone or in combination with the second as a distinguishing feature of object oriented programming, while other programming languages which provide lexical closures view encapsulation as a feature of the language orthogonal to object orientation.
In a lighter note: Encapsulation is summation of the below written conclusion:
1. Grouping functions and corresponding data into a single class.
  •  Every java class is an example of encapsulation, which can be said to be encapsulated component
  • Every package is an example of encapsulation mechanism.
2. Every class follows encapsulation if it follows the below written principal

  •       Encapsulation=Data hiding+Abstraction
  •       It must hide data behind methods          

Let us discuss one by one:
I am going to tell you the process and guideline how to make better encapsulation in Java..

  • Make your instance variable always private
  • Always create a setter and getter method that is of public.So if you have n number of instance variables you will create 2*n number of methods
  •  So any object trying to access the variables has to route through setter and getter method.
  • Setter and getter methods will give you more security over normal deceleration.

public class settergetter {

  * @param args
 private String pencolor;
 private int pensize;
 public static void main(String[] args) {
  // TODO Auto-generated method stub


 public String getPencolor() {
  return pencolor;

 public void setPencolor(String pencolor) {
  this.pencolor = pencolor;

 public int getPensize() {
  return pensize;

 public void setPensize(int pensize) {
  this.pensize = pensize;


As the pensize and pencolor variables are private in nature so it can not be directly accessed by outside this class but the setter and getter methods are public in nature which can be accessed by outside of the class.

so you can modify the setter method as per your if you want restrict users to set pensize or pencolor to a particular value you can modify the setter method accordingly with a simple if else logic.

Let us discuss second point abstraction:
It enables us to highlight only few services may be via methods but hides actual implementation of it.
There are two ways by which we can offer abstration:
  • interfaces
  • Abstract class
In our previous example ..only setter and getter methods are exposed to outside world. But how they work is beyond the reach of the user. So if you change the implementation of setPensize method the outside user will not be able to perform any activity rather they have to abide by the rule you have set 

public void setPensize(int pensize) {
if (pensize>5)
  this.pensize = pensize;
this.pensize = 5;

In this example the setter method is modified in such a way if the service consumer calls this method to set Pen size, it will not be able to set size below 5.So we are providing more security.

Second point could be , it my requirements are going to change in some other manner, the consumer will not be impacted rather the implementation will be changed internally

Advantages of Abstraction:(Eventually it becomes advantages of Encapsulation)
  • Provide Security
  • Enhancement becomes very easy.
  • Improves maintainability and more modularity
 Disadvantages of Abstraction:(Eventually it becomes disadvantages of Encapsulation)

  • The code becomes lengthy
  • Slows down the execution
The Golden Rules of Encapsulation Concept of Encapsulation The Golden Rules of Encapsulation Concept of Encapsulation Reviewed by Animesh Chatterjee on March 30, 2013 Rating: 5

No comments:

Powered by Blogger.