Chapter 11: Access Modifiers and Encapsulation

1. What is Encapsulation? (Super Simple Analogy)

Encapsulation means:

  • Hiding the internal details of how an object works
  • Exposing only the necessary parts to the outside world
  • Protecting the data so it can only be changed in controlled ways

Think of a TV remote:

  • You can press Power, Volume Up, Channel → these are the public methods/properties you’re allowed to use
  • You cannot open the TV and directly change the internal circuits → that’s private/internal stuff

This way, the TV works safely even if you misuse the remote!

In C#, we achieve encapsulation using access modifiers and properties.

2. All Access Modifiers Explained (With Real Examples)

Modifier Accessible from Common Use Case Real-World Analogy
public Anywhere (same project, other projects) Methods & properties users should call Remote control buttons
private Only inside the same class Internal fields, helper methods Internal wires & circuits of the TV
protected Inside the class and derived classes Fields/methods that children should access Protected parts that family members can touch
internal Only inside the same assembly (project) Types/methods used only within your project Internal company tools – not for outsiders
protected internal Inside the class, derived classes, and same assembly Rare – when you want both inheritance & assembly access Special internal family tools
private protected Inside the class and derived classes in the same assembly (C# 7.2+) Very rare – protected but only within assembly Family tools that only close relatives can use

Quick rule of thumb (what most professionals follow):

  • Make fields → private
  • Make public methods & properties → public
  • Use protected only when you plan to inherit
  • Use internal when you want to hide something from other projects (DLLs)

3. Properties with Getters & Setters – The Heart of Encapsulation

Never expose fields directly! Always use properties to control access.

A. Auto-implemented Property (Simple & Clean)

C#

B. Full Property with Validation (Real Encapsulation)

C#

Usage:

C#

4. Real-Life Example – Bank Account with Proper Encapsulation

C#

5. Mini-Project: Secure Employee Class

C#

Summary – What We Learned Today

  • Encapsulation = hide data + expose controlled access
  • public → everyone can use
  • private → only inside class
  • protected → class + derived classes
  • internal → same assembly/project
  • Properties → best way to control access (get/set with validation)
  • Never expose fields directly – always use properties!

Your Homework (Super Practical!)

  1. Create a new console project called EncapsulationMaster
  2. Create a class Product with:
    • Private fields: _id, _price, _stock
    • Public properties: Name, Description
    • Read-only: Id, Price, Stock
    • Methods: ReduceStock(int quantity) (with checks)
    • IncreasePrice(decimal percentage) (with validation)
    • Constructor that sets everything safely
  3. In Program.cs: Create 2–3 products, try to buy some stock, increase price, and show info

Next lesson: Inheritance – we’re going to learn how classes can inherit from each other and become even more powerful!

You’re doing absolutely fantastic! 🎉 Any part confusing? Want more examples with protected or internal? Just tell me — I’m right here for you! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *