Encapsulation is nothing but wrapping up of data and its related functionalities in to a single unit. Encapsulation is introduced mainly to overcome problem with global variables in structured programming languages. Java developers developed the concept of Encapsulation. For instance, have a structured programming language program with twenty global variables and eight functions.
It is in no doubt that all the eight functions will not utilize all the available global variables. Five of the global variables may be used by three functions. But in a structured programming language like c it is not achievable to control the access of global variables by some limited number of functions. Every function will have access to all the available global variables.
How To avoid global variables problem
To avoid this global variables problem programmers have designed a way such that the variables and the functions which are connected with each other or the functionalities operate on those variables are enclosed In a block and that block is called a class and that class is giving a name, just as a function is given a name. Now the variables inside the class cannot be called as a local variable because they are not local to only one function and they cannot be called a s global variables because they are confined to a block and not global hence these variables are known as instance variables.
Encapsulation is the concept of binding data along with the corresponding functionalities; encapsulation came into existence in order to make available security for the data present inside the program. Any object oriented programming language file look like a group of classes, everything is encapsulated and nothing is outside the class.
Encapsulation is the backbone of Object oriented programming languages, java programming supports all object oriented programming language concepts like inheritance, encapsulation, data abstraction and polymorphism and for this reason it is recognized as Object oriented programming language. C++ breaks the idea of encapsulation, for the reason that the main () method in a C++ program is declared outside a class hence it is not a pure Object oriented programming language.