1Definition
Encapsulation is the mechanism of wrapping data (variables) and code (methods) together into a single unified unit called a Class, while restricting direct external access to internal component states.
2Simple Explanation
Think of a Capsule Medical Pill. Inside the hard outer gelatin shell, several active medicinal powders are blended together. You consume the single capsule whole without needing to handle or mix the loose powders manually. Encapsulation binds variables and methods into one container (the class capsule) and seals the data inside.
3Real-Life Example / Analogy
- School Bag: A school bag encapsulates your books, pens, erasers, and lunch box into a single carrying unit.
- Smart Phone: The phone encases screen controls, battery power management, and cellular chips. Users operate the phone via screen touches (public interface) without touching internal circuits.
4Diagram / Visual Representation
5Working / How It Works
- Declare class attributes as
private. - Provide
publicgetter functions to read attribute values. - Provide
publicsetter functions to update attribute values under strict validation logic. - External callers interact exclusively with the public member functions.
6Types / Components / Characteristics
- Key Characteristics:
- Bundling: Blending attributes and behaviors into a single class entity.
- Data Hiding: Masking internal state variables behind
privateaccess specifiers. - Controlled Access: Utilizing getters/setters for read/write access.
- Encapsulation vs Data Hiding:
- Encapsulation focuses on binding data and methods.
- Data Hiding focuses on restricting access to data.
7Solved Examples / Problems
Problem Statement: Write a C++ class representing an Employee that encapsulates salary details.
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
double salary; // Encapsulated data
public:
void setSalary(double s) {
if (s > 0) salary = s;
}
double getSalary() {
return salary;
}
};
int main() {
Employee emp;
emp.setSalary(75000);
cout << "Employee Salary: $" << emp.getSalary() << endl;
return 0;
}Output:
Employee Salary: $750008Advantages, Disadvantages & Applications
- Advantages:
- Read-Only or Write-Only field access can be achieved by omitting setters or getters.
- Unit testing is simplified because classes are self-contained.
- Prevents unintended side-effects across codebase modules.
- Disadvantages:
- Increases line count due to boilerplate getter/setter declarations.
- Applications: User authentication modules, payment gateway API wrappers.
9Important Points to Remember
- Encapsulation is achieved by combining Data Hiding (
privatemembers) with Public Member Methods. - It enforces the principle of least privilege in software architecture.
10Test Your Understanding
- Conceptual Question: How does Encapsulation facilitate the creation of a Read-Only class attribute?
Show answer
By defining aprivatevariable and exposing only apublicgetter function without providing any setter function. - MCQ: Encapsulation is best described as:
- A) Deriving child classes from parent classes
- B) Binding data and functions together into a single unit
- C) Overloading functions with different signatures
- D) Re-defining base class functions in child classes
Show answer
B) Binding data and functions together into a single unit - Application Question: Write a setter for
agethat rejects values below 0 or above 120.Show answer
void setAge(int a) { if(a >= 0 && a <= 120) age = a; }
