Encapsulation involves the grouping of related concepts into one unit. This allows it to be referred to by a single name. Encapsulation is writing code so that instead of repetition in different parts of the program the source code is factored out, replaced in a single piece as a function. As such, the function acts on the simplest level of encapsulation and abstraction. In this way, a programmer can refer to the function by name as a single entity. The programmer can even forget the details of the internals and only remember its interface.
The next level of encapsulation is the Object Oriented world, which expands on this concept from the subroutine level to the object level. The attributes that represent the state of an object and the methods that work on them are packaged into an object type.
Encapsulation is one of the main features of Object Oriented Programming, an alternative model to programming style.
Source: What is Encapsulation
Problem: Lack of Encapsulation
But while the encapsulation concept is a powerful working tool, its lack of availability in structured programming means that programs will be longer. The same or similar code will appear in more than one location. This also means that the programs will have a greater chance of errors. The testing will be lengthy as well since every piece of code will have to be tested. Even if the code is without errors in one place, the same piece of code may appear in a different part of the program and could have problems there.
Problem : Same Code Repetition
Because the code that is written may appear in different parts of the program, it can be vulnerable to different problems because of its location. Programs have variables, which means that they can take on different values at different parts of the program. So the testing that is necessary to develop an error-free program can be time consuming.
Lack of Information Hiding
Information hiding involves isolating design decisions in a computer program that have the greatest chance to change. This protects other parts of the program from modifications if the design decision is changed. The protection involves providing a stable interface (a point of interaction between components, either hardware or software) which protects the rest of the program from the details that are most likely to change.
However, structured programming has no such control. The possibility of spillover from the effects of coding to other areas is easily possible. For example, once code is executed in one part of the program, variables that have a value may clash with the same variable in another part of the program. The possibility that the appearance of the first variable will dominate the second appearance may cause serious errors. Worse, the debugging efforts might be stymied since the code will look correct. You cannot hide the results from one part of the program as they may influence another part.
Encapsulation and Information Hiding - An Example
The following code represents encapsulation. Here is an example of a geometrical concept (the point in a 2D space).
1. public class Points
3. public Double A;
4. public Double B;
The following program code is an example of information hiding. It is applied to the same concept:
1. public interface Points
3. Double GetA();
4. Double GetB();
5. void SetCartesian(Double A, Double B);
6. Double GetX();
7. Double Get Beta();
8. void SetPolar(Double X, Double Beta);
In this second example, the actual implementation is hidden. The client will not know whether the internal representation of the point is in the form of a rectangular or polar coordinate.
These pieces of code, however, have to be re-used again and again in a program. The advantage of encapsulation or information hiding is not available.
The disadvantages of structured programming are lack of encapsulation, lack of information hiding, repetition, and having a longer program than is necessary. Error control may be harder to manage; debugging efforts can be stymied because the problem code will look right and even perform correctly in one part of the program but not in another.
Object Oriented Programming and Microsoft’s .Net interface programming model are used to avoid many of the problems associated with structured programming.