OOPS Encapsulation
OOPS
2 Articles
In this article, let's learn about Encapsulation
in Object Oriented Programming.
Table of Contents
- What is Encapsulation?
- How Encapsulation helps to Enforce Invariants?
- How to introduce Encapsulation?
- Summary
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? Encapsultion
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 internalBundling 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.
Code Sample - OOPS Encapsulation Example
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).
Code Sample - Not properly Encapsulated
Demo - Not properly Encapsulated
Scenario - Let's try creating a square class that violates invariant edges.Length == 4
.
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 entring 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.
Code Sample - Properly Encapsulated
Demo - Properly Encapsulated
Scenario - Let's try creating a square class that violates invariant edges.Length == 4
.
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 metal 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.