

Bundling: Combining data (instance variables) and methods (that operate on that data) into a class.
Data Hiding: Making the instance variables of a class private (using the private access modifier). This prevents direct access to the variables from outside the class.
Access Control: Providing public methods (getters and setters) to access and modify the private instance variables. This allows controlled access to the data.
The following are the steps to implement the encapsulation in java.

Declare Instance Variables as private: This is the most important step. Making the fields private prevents direct access from outside the class.
Provide Public Getter Methods: Getter methods (also called accessor methods) are used to retrieve the values of the private instance variables. They typically have a name like getVariableName().
Provide Public Setter Methods: Setter methods (also called mutator methods) are used to modify the values of the private instance variables. They typically have a name like setVariableName(). You can add validation or logic within the setter methods to control how the data is modified.
class BankAccount {
private double balance;
public void setBalance(double initialBalance) {
balance = initialBalance;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
} else {
System.out.println("Invalid deposit amount.");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient funds or invalid withdrawal amount.");
}
}
}
public class EncapsulationExample2 {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000);
account.deposit(500);
account.withdraw(2000); // This will print "Insufficient funds..."
System.out.println("Balance: " + account.getBalance());
}
}
Here, balance is private. deposit() and withdraw() methods control how the balance is changed, ensuring that only valid operations are performed.
Benefits of Encapsulation:
Encapsulation is a crucial concept in OOP, promoting well-structured, robust, and maintainable code. It’s a best practice to always make instance variables private and provide public getter and setter methods to control access to them.
Design a Book class for a library management system. The class should have the following attributes:
title (String)author (String)isbn (String)isBorrowed (boolean)Implement encapsulation principles for all attributes. Provide appropriate getter and setter methods. Specifically, the setIsBorrowed() method should have the following logic:
isBorrowed is set to true), record the current date and time (you can use java.time.LocalDateTime for this). Store this information in a private borrowDate attribute (add this attribute to the Book class).isBorrowed is set to false), set the borrowDate to null.Implement a Library class that manages an array of Book objects. The Library class should have methods to:
Demonstrate the functionality of the classes in a Main class.
Design an Employee class for a payroll system. The class should have the following attributes:
name (String)employeeId (int)hourlyRate (double)hoursWorked (double)Implement encapsulation for all attributes. Provide appropriate getter and setter methods. Implement data validation within the setter methods:
employeeId should be a positive integer.hourlyRate should be a positive double.hoursWorked should be a non-negative double. If hoursWorked exceeds 60, print a warning message (but still set the value).Add a method calculateSalary() to the Employee class that calculates the employee’s salary based on hourlyRate and hoursWorked. If hoursWorked is greater than 40, pay 1.5 times the hourlyRate for the overtime hours (hours above 40).
Create a Payroll class that manages an array of Employee objects. The Payroll class should have methods to:
Demonstrate the functionality of the classes in a Main class. Include test cases that demonstrate the data validation and overtime calculation.
You must be logged in to submit a review.