Topic 4

Encapsulation

2 min read3 questions to test yourself

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

Encapsulated Class

controlled access

Public methods
getBalance(), deposit(), withdraw()

Private data
accountBalance
accountNumber

5Working / How It Works

  1. Declare class attributes as private.
  2. Provide public getter functions to read attribute values.
  3. Provide public setter functions to update attribute values under strict validation logic.
  4. 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 private access 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.

C++
#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:

Output
Employee Salary: $75000

8Advantages, 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 (private members) with Public Member Methods.
  • It enforces the principle of least privilege in software architecture.

10Test Your Understanding

  1. Conceptual Question: How does Encapsulation facilitate the creation of a Read-Only class attribute?
    Show answer
    By defining a private variable and exposing only a public getter function without providing any setter function.
  2. 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
  3. Application Question: Write a setter for age that rejects values below 0 or above 120.
    Show answer
    void setAge(int a) { if(a >= 0 && a <= 120) age = a; }

Finished this topic?

0 of 14 topics read in Object-Oriented Programming

Handwritten notes for the exam

Unit-wise SPPU notes and PYQs for this subject.

Helpful? Share it with your class

Free for everyone, no login needed.

Share topic