1. Introduction
Encapsulation is a fundamental concept in object-oriented programming (OOP). It refers to the bundling of data and methods that operate on that data into a single unit, often termed as an object. In this post, we will explore the concept of encapsulation in Ruby and its significance in maintaining the integrity of an application.
Definition
Encapsulation is the practice of hiding the internal workings of an object from the outside world and only exposing a controlled interface. This ensures that the object's data can only be modified in well-defined ways, preventing unauthorized or unintended changes. In Ruby, this is achieved using private, protected, and public access modifiers.
2. Program Steps
1. Create a class with instance variables.
2. Define public methods that serve as the interface to interact with the object.
3. Define private methods that are inaccessible from outside the class.
4. Instantiate the class and try accessing its methods.
3. Code Program
# Define a class named BankAccount
class BankAccount
# Initialize the BankAccount with a balance
def initialize(balance)
@balance = balance
end
# Public method to deposit money
def deposit(amount)
if valid_transaction?(amount)
@balance += amount
"Deposited #{amount}. New balance: #{@balance}."
else
"Invalid transaction."
end
end
# Public method to withdraw money
def withdraw(amount)
if valid_transaction?(amount) && sufficient_balance?(amount)
@balance -= amount
"Withdrew #{amount}. New balance: #{@balance}."
else
"Invalid transaction."
end
end
private
# Private method to check the validity of the transaction amount
def valid_transaction?(amount)
amount > 0
end
# Private method to check if there's sufficient balance
def sufficient_balance?(amount)
@balance >= amount
end
end
# Create an instance of the BankAccount class with a balance of 1000
account = BankAccount.new(1000)
# Deposit and print the result
puts account.deposit(200)
# Withdraw and print the result
puts account.withdraw(50)
# Try to access a private method (This will raise an error if uncommented)
# puts account.valid_transaction?(100)
Output:
Deposited 200. New balance: 1200. Withdrew 50. New balance: 1150.
Explanation:
1. We defined a BankAccount class that represents a simple bank account with a balance.
2. Inside the class, we created public methods deposit and withdraw that allow interactions with the bank account.
3. We also defined private methods valid_transaction? and sufficient_balance? to handle internal checks. These methods are encapsulated within the class and are not exposed to the external world.
4. When we created an instance account of the BankAccount class and interacted with it using the deposit and withdraw methods, it internally used private methods to validate the transactions.
5. The line attempting to access the private method valid_transaction? is commented out because it will raise an error if executed. This demonstrates the concept of encapsulation, as internal methods and data are protected from direct external access.
Through encapsulation, Ruby allows us to maintain the integrity of our objects by controlling how their data is accessed and modified.