Topic 3

Access Specifiers & Data Hiding

3 min read3 questions to test yourself

1Definition

Access Specifiers (or Access Modifiers) are keywords in object-oriented languages that define the accessibility or scope of class members (data members and member functions) from outside the class. Data Hiding is the security technique of making class attributes private so they cannot be directly viewed or manipulated from external scopes.

2Simple Explanation

Access specifiers act like Security Badges in an office building:

  • private: Executive office—only accessible by employees working in that specific office.
  • protected: Family lounge—accessible by current office employees and their child branches.
  • public: Main reception lobby—open to any member of the general public.

3Real-Life Example / Analogy

In a Bank Account:

  • private: Your account balance and PIN (hidden from public view).
  • protected: Internal credit score rules (accessible by parent bank and child branch entities).
  • public: Bank customer service hotline and branch address (accessible to everyone).

4Diagram / Visual Representation

Class Scope

Private members
Accessible only inside the class

Protected members
Accessible inside class and derived classes

Public members
Accessible from anywhere

5Working / How It Works

  1. During compilation, the compiler enforces access check rules.
  2. If code outside the class tries to access a private or protected member directly, the compiler flags a syntax error (error: ‘int Account::balance’ is private).
  3. Access to private data is funneled through public Getter and Setter member functions, enabling input validation.

6Types / Components / Characteristics

  • Access Level Comparison:
Access Specifier Inside Same Class Derived (Child) Class Outside Class / Main
private Yes No No
protected Yes Yes No
public Yes Yes Yes

Note: Language extensions (like C# or Java) include internal (same assembly/package) and protected internal.

7Solved Examples / Problems

Problem Statement: Demonstrate how private access specifiers implement data hiding and how getter/setter methods protect member variables.

C++
#include <iostream>
using namespace std;
class BankAccount {
private:
    double balance; // Hidden data
public:
    void setBalance(double b) {
        if (b >= 0) { // Validation
            balance = b;
        } else {
            cout << "Invalid Balance!" << endl;
        }
    }
    double getBalance() {
        return balance;
    }
};
int main() {
    BankAccount acc;
    acc.setBalance(5000.50);
    cout << "Account Balance: $" << acc.getBalance() << endl;
    // acc.balance = -100; // Compile Error!
    return 0;
}

Output:

Output
Account Balance: $5000.5

8Advantages, Disadvantages & Applications

  • Advantages:
    • Protects internal data integrity against unintended corruption.
    • Allows internal class implementation to change without breaking external client code.
  • Disadvantages:
    • Slight overhead due to indirect method calls for simple variable reads.
  • Applications: Secure financial transaction processing, password hashing storage modules.

9Important Points to Remember

  • In C++, the default access specifier for a class is private, whereas for a struct it is public.
  • Data hiding is not data encryption; it is an architecture-level scope constraint enforced at compile time.

10Test Your Understanding

  1. Conceptual Question: What is the key structural difference between a class and a struct in C++ regarding access specifiers?
    Show answer
    Class members default to private access, whereas struct members default to public access.
  2. MCQ: Which access specifier allows member access to derived child classes but blocks access from the main() function?
    • A) public
    • B) private
    • C) protected
    • D) internal
    Show answer
    C) protected
  3. Application Question: Fix the error in class X { int a; }; int main() { X obj; obj.a = 10; }.
    Show answer
    Declare public: before int a; or add a public setter method, as int a; is private by default.

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