
OOPS Encapsulation
Author - Abdul Rahman (Bhai)
OOPS
2 Articles
Table of Contents
What we gonna do?
In this article, let's learn about Encapsulation in Object Oriented Programming.
Why we gonna do?
When class state can be set freely from anywhere in a codebase, nothing prevents an object from entering an invalid or inconsistent state. A Square with two edges, a negative age, or an email address without an @sign — these are examples of data integrity violations that become possible when a class has no protection around its internal state.
The longer a codebase grows without encapsulation, the harder it becomes to trust the state of any given object. Debugging turns into a search across many call sites to find where invalid data was introduced.
Encapsulation solves this by making a class responsible for its own validity. This eliminates an entire category of bugs and dramatically reduces the mental overhead of working with shared code.
How we gonna do?
What is Encapsulation?
We often hear that Encapsulation means Information hiding and Bundling of data and operations. Although these two techniques help achieve encapsulation they themselves are not the definition of Encapsulation. So what it is then? Encapsulation is the act of protecting data integrity. A class is properly encapsulated when its internal data cannot be set to invalid or inconsistent state. As I mentioned Information hiding and Bundling of data and operations helps to achieve that.
- Information hiding - Less risk of corrupting the class's internal
- Bundling of data and operations - Perform integrity checks before modifying the data such that there is a single entry point to do actions
Let's see how to model and Encapsulate a class properly such that it doesn't bring the class to inconsistent state.
How Encapsulation helps to Enforce Invariants?
You can also think of Encapsulation as invariants. Each class will have its own set of invariants that must be held true at all times. Your responsibility as a software developer is to maintain the invariants and the best way to do so is encapsulate your classes so that it wont give you a chance to violate that.
For example, there could be class Square in your application. This class represents a concept that could have 4 edges edges.Length == 4. This could should always be true for all the squares. The Square class looks like the below code.
public class Square {
public IEnumerable<Edge> Edges { get; }
public Square(IEnumerable<Edge> edges) {
Edges = edges;
}
}
This class is not properly encapsulated because there is nothing preventing us from creating an instance which has two edges (line) or three edges (triangle).
var line = new Square(twoEdges);
var triangle = new Square(threeEdges);
How to introduce Encapsulation?
How to fix the above violation? Let's try to introduce Encapsulation in class Square such that it will not allow us to violate the invariant. To prevent instances of this class from entering invalid state, we can add a Guard Clause in this class's constructor. With this check we can throw an exception when a Square is created with incorrect number of edges. This version of class is now properly Encapsulated because it protects itself from having fewer or more than 4 edges.
public class Square {
public IEnumerable<Edge> Edges { get; }
public Square(IEnumerable<Edge> edges) {
if (edges.Count() != 4) { //-|
throw new Exception("Square must have 4 edges"); // |-- Guard Clause
} //-|
Edges = edges;
}
}
Summary
In this post, we learn't what is encapsulation in oops and the principle behind it. Encapsulation is the act of protecting data integrity. A class is properly Encapsulated when its internal data cannot be set to invalid or inconsistent state. The conditions that define the class state as valid are called invariants and must be held true at all the times. The two techniques that help to achieve Encapsulation are Information hiding and Bundling data and operations together. The purpose of Encapsulation is to reduce the mental burden required to maintain data consistency. Encapsulation and Abstraction are highly interconnected but still have a different principle. In our next article we'll learn what is abstraction.