1In object-oriented programming, encapsulation refers to the bundling
2of data with the methods that operate on that data, or the restricting
3of direct access to some of an object's components
1Encapsulation:
2Encapsulation is the mechanism of hiding of data implementation by
3restricting access to public methods. Instance variables are kept
4private and accessor methods are made public to achieve this.
1Encapsulation focus on Private Data
2Encapsulation allows the programmer to hide
3and restrict access to data.
4To achieve encapsulation:
51.Declare the variables with the private access modifier
62.Create public getters and setters that allow indirect
7access to those variables
8
9Framework Example:
10In my project I created multiple POJO/BEAN classes in order
11to manage test data and actual data.
12I take JSON from API response and convert
13to object of my POJO class.
14All the variables are private with getters and setter.
15Also in order to store credentials or sensitive data
16in my framework I have use encapsulation, configuration reader
17also known as property file or excel sheet to hide data
18from the outside. I use Apache POI if the data is stored in Excel
19in order to extract/read and modify data.
20Partial Encapsulation Usage / getters and setters have to be Public
21We can provide only getters in a class to make the class immutable.
22(Read only) returning private instance variable that’s all
23We provide setters in a class to make the class attribute write-only,
24and return type is void just initialize the given argument
25
1Encapsulation focus on Private Data
2Encapsulation allows the programmer to hide
3and restrict access to data.
4To achieve encapsulation:
51.Declare the variables with the private access modifier
62.Create public getters and setters that allow indirect
7access to those variables
8
9Framework Example:
10In my project I created multiple POJO/BEAN classes in order
11to manage test data and actual data.
12I take JSON from API response and convert
13to object of my POJO class.
14All the variables are private with getters and setter.
15Also in order to store credentials or sensitive data
16in my framework I have use encapsulation, configuration reader
17also known as property file or excel sheet to hide data
18from the outside. I use Apache POI if the data is stored in Excel
19in order to extract/read and modify data.
20Partial Encapsulation Usage / getters and setters have to be Public
21We can provide only getters in a class to make the class immutable.
22(Read only) returning private instance variable that’s all
23We provide setters in a class to make the class attribute write-only,
24and return type is void just initialize the given argument
1#include<iostream>
2using namespace std;
3class ExampleEncap{
4private:
5 /* Since we have marked these data members private,
6 * any entity outside this class cannot access these
7 * data members directly, they have to use getter and
8 * setter functions.
9 */
10 int num;
11 char ch;
12public:
13 /* Getter functions to get the value of data members.
14 * Since these functions are public, they can be accessed
15 * outside the class, thus provide the access to data members
16 * through them
17 */
18 int getNum() const {
19 return num;
20 }
21 char getCh() const {
22 return ch;
23 }
24 /* Setter functions, they are called for assigning the values
25 * to the private data members.
26 */
27 void setNum(int num) {
28 this->num = num;
29 }
30 void setCh(char ch) {
31 this->ch = ch;
32 }
33};
34int main(){
35 ExampleEncap obj;
36 obj.setNum(100);
37 obj.setCh('A');
38 cout<<obj.getNum()<<endl;
39 cout<<obj.getCh()<<endl;
40 return 0;
41}
1In object-oriented programming, encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object's components