25 Oct 2023
Encapsulation is the concept of wrapping data (attributes or properties) and methods (functions or behaviors) into a single unit called a class.The purpose of encapsulation is to prevent direct access to data from outside. Instead, the data can only be accessed or modified through the class's getter and setter methods. Encapsulation is a way of protecting data and providing controlled access, ensuring that data is only accessed according to the class's public interface.
In object-oriented programming (OOP), encapsulation is one of the fundamental principles that promotes data hiding and access control. It helps in managing the accessibility of class members (variables and methods) by using access modifiers and often involves the use of getters and setters to manipulate the private data members of a class.
In encapsulation:
-
Data hiding: The internal state of an object is hidden from the outside world. This means that the data can only be accessed and modified through the methods provided by the class. This prevents direct manipulation of the object's state, which helps to maintain the integrity of the data and ensures that it remains consistent.
-
Access control: Encapsulation allows the class to control how its data is accessed and modified. By defining appropriate access modifiers (such as public, private, or protected), the class can restrict access to certain data or methods, thereby preventing unauthorized or unintended use.
Examples of encapsulation in Object-Oriented Programming (OOP) :
Example 1: Encapsulation in a Car
Imagine a car as an object with encapsulation:
-
The car's internal engine components and mechanisms are hidden from the driver and passengers. They don't need to know how the engine works; they just interact with the car's interface, such as the steering wheel, pedals, and gearshift.
-
The car's "start" button is a method of encapsulation. It encapsulates the complex process of starting the engine behind a simple action. The driver doesn't need to understand the inner workings of the engine to start the car.
class Car:
private engine
method start():
if engine.isFunctional() and fuelTank.hasFuel():
engine.turnOn()
else:
displayMessage("Cannot start the car.")
method accelerate():
if engine.isRunning():
// Acceleration logic here
method brake():
if engine.isRunning():
// Braking logic here
In this pseudocode, the car's engine and its internal details are encapsulated. The start method encapsulates the complex engine startup process and checks for fuel and engine functionality. The accelerate and brake methods allow the driver to control the car without needing to understand the engine's inner workings.
Example 2: Encapsulation in a Bank Account
Consider a bank account with encapsulation:
-
The account holder's personal information, such as their social security number and home address, is not exposed publicly. It is encapsulated within the bank's systems and only accessible by authorized personnel.
-
Deposits and withdrawals can be seen as methods of encapsulation. They allow the account holder to interact with their account's balance without having direct access to the bank's internal accounting systems.
class BankAccount:
private balance
private accountHolder
method deposit(amount):
if amount > 0:
balance += amount
method withdraw(amount):
if amount > 0 and amount <= balance:
balance -= amount
method getBalance():
return balance
In this pseudocode, the bank account's balance and account holder's information are encapsulated. The deposit and withdraw methods provide controlled access to the balance, ensuring valid transactions. The getBalance method allows account holders to check their balance without accessing it directly.
In all three examples, encapsulation ensures that the inner workings and sensitive information are hidden and can only be accessed through well-defined and controlled methods or interfaces, providing security, abstraction, and ease of use.