Encapsulation ensures that each object in your code should control its own state. Encapsulation is defined as the wrapping up of data under a single unit. It binds together code and the data it manipulates. It is a technique of restricting a user from directly modifying the data members or variables of a class in order to maintain the integrity of the data. How do we do that? We restrict the access of the variables by switching the access-modifier to private and exposing public methods that we can use to access the data.
Note: Examples of this and its related articles are related to high level statically typed object oriented languages such as C# and Java.
How many ways can we implement encapsulation?
Implement — using private access modifier
In C# Encapsulation is implemented by following these steps:
- By declaring the variables as private (to restrict its direct access from outside the class)
- By defining one pair of public setter and getter methods or properties to access private variables.
Example — using private access modifier
Note that this can be done using Properties and its getter and setter as well. The important thing is to prevent direct access to data in an object. By creating getter and setter, we can perform validations before saving the data.
Let’s take an example where a class Person has a property for Age that age should be more than -1. So we will apply private modifier on setter of Age property, and when user wants to change the Age.
Implement encapsulation with the benefit of abstraction
Let’s take an example where a class Person has a property for Age that should be dependent on the date of birth so we want to prevent user from modifying Age but user can see the Age.
Here’s where it relates to abstraction: The data in a class is hidden from other classes using the data hiding concept which is achieved by making the members or methods of a class private, and the class is exposed to the end-user or the world without providing any details behind implementation using the abstraction concept, so it is also known as a combination of data-hiding and abstraction.
Abstraction vs. Encapsulation
In the popular programming text Object-Oriented Analysis and Design, Grady Booch writes that:
Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object…encapsulation focuses on the implementation that gives rise to this behavior.
Stated differently, an abstraction relates to how an object and its behaviors are presented to the user and encapsulation is a methodology that helps create that experience.
Modern example of Encapsulation - Containerization
Containers are a relatively new type of software that can be used to virtually package a piece of code along with all of its libraries and other dependencies that it needs to execute. Containers create an encapsulated virtual environment where an application can be launched using the minimum amount of storage space and computing power. A group of containers can share access to a single operating system and draw their computing resources from a single piece of hardware.
Thank you for reading this article. I hope this and its related articles will help you start your programming journey.
The Four Pillars of Object-Oriented Programming
What is Encapsulation in OOP? | Sumo Logic
Encapsulation in OOP Meaning: In object-oriented computer programming languages, the notion of encapsulation (or OOP…
Encapsulation in C#
Back to: C#.NET Tutorials For Beginners and Professionals In this article, I am going to discuss the Encapsulation in…
Disclaimer & fair use statement:
This website may contain copyrighted material, the use of which may not have been specifically authorized by the copyright owner. This material is available in an effort to explain concept of Object Oriented Programming designs in an articulate and summarized manner to be used as an educational tool. The material contained in this website is distributed without profit for research and educational purposes. Only small portions of the original work are being used and those could not be used easily to duplicate the original work. This should constitute a ‘fair use’ of any such copyrighted material (referenced
and provided for in section 107 of the US Copyright Law). If you wish to use any copyrighted material from this site for purposes of your own that go beyond ‘fair use’, you must obtain expressed permission from the copyright owner.