My good friend Sam Stokes posted an introduction to Encapsulation not long ago. Not that I am competitive mind you but I thought I could do better. And since I had some vacation time in which to think and write I have given it a try. I’ll let you judge if I succeed or not.
So what is Encapsulation? Encapsulation is an important part of object oriented programming. It is part of what makes OOP “safer” and more easy to use. Briefly stated Encapsulation is the hiding of implementation details of a class. Specifically you include the data storage as private data and create methods that interact with that data. The implementation of these methods is not visible to the programmer who uses the class. Nor is the internal representation of the data. Everything is in this software capsule – this “black box” that just works. One fun example is a die class (Die being the singular of dice in this case. Nothing to do with first person shooter actions)
Of the several benefits of encapsulation are that data cannot be directly manipulated by code that calls/uses the object and that the way methods work can be changed without negative impact on other code that uses the class/object. Let’s take a look at this in code with the first part of a very simple Die class (This example is in Visual Basic but the concepts are the same in other programming languages)
In this example we only allow the user to get (and change in a protected fashion) the face value of the Die. The property FaceValue is read only – we do not want the user code changing that to some value of its own choosing. In fact the only way to change the value is to call the Roll function/.method which uses random values that are (theoretically at least) outside the user’s control. The ToString method is likewise only a way to get the face value.
In our example there is no way to retrieve the number of sides the Die has and fixing that would probably be a good edition. Like FaceValue that would be done as a read-only Property.
In our example we could substitute any algorithm for changing the face value of our Die that we wanted to as long as it returned an Integer value within the range expected by the user code. While we are probably not going to write our own random number routine we could and it would not require any changes in code that used this Class. In other applications being able to change the algorithm used in a method without having to change code that uses it could be a valuable option. It would allow us to test various algorithms for performance, accuracy, or other values quickly and easily. In a payroll program we could make changes in the calculations for bonuses, overtime or any number of things within a single class without having any negative impact on programs that used that class. That’s a big win in ease of testing and debugging. It’s all made possible by encapsulation.