Encapsulation

Learn how encapsulation keeps internal details private and exposes a clean interface in BornomalaScript.

Encapsulation

Encapsulation means keeping an object’s internal data and behavior together while controlling how the outside world interacts with it.

In simple terms, it helps you protect important values and expose only the actions that should be used.

Why Encapsulation Matters

Without encapsulation, any part of a program could change data directly. That can make code harder to manage and easier to break.

Encapsulation helps you:

  • keep related code together
  • protect data from accidental changes
  • make your API easier to understand
  • reduce the chance of bugs

Example Idea

Imagine a bank account object.

You might want people to deposit or withdraw money, but you do not want them changing the balance directly.

That is the kind of problem encapsulation solves.

Conceptual Example

dhoro account = {
	balance: 1000,
	deposit: kaj(amount) {
		balance = balance + amount
	},
	withdraw: kaj(amount) {
		balance = balance - amount
	}
}

In a real program, you would use the methods to change the balance instead of touching it from everywhere.

How It Works

Encapsulation groups the data and the actions that work on that data.

That means:

  • data stays organized
  • methods control the changes
  • outside code uses a clean interface

Step-by-Step Breakdown

  1. Create an object to represent one thing
  2. Store its data inside the object
  3. Add methods that change or use the data
  4. Call the methods instead of editing the data directly

Common Mistakes

Beginners often make these mistakes:

  • putting unrelated values in the same object
  • changing internal data from random places in the program
  • forgetting that methods should manage the object’s state

Practice Task

Try designing an object for:

  1. A student record
  2. A bank account
  3. A shopping cart

For each one, think about what data should stay inside the object and what actions should be exposed.

Quick Checklist

Before moving on, make sure you can:

  • explain why encapsulation is useful
  • describe the difference between data and methods
  • understand why some values should not be changed directly

If yes, you are ready for abstraction.