Appearance
Encspsulation in Java
Think of it like this: Imagine you have a treasure chest full of gold coins. You don't want just anyone to come along and grab your gold, right? You want to keep it safe. Encapsulation is like putting a lock on that treasure chest so only you, or those you trust, can access the gold inside.
Now, let's see how this works with a fun example:
java
public class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Yay! You deposited $" + amount + "!");
} else {
System.out.println("Oops! That's not a valid amount to deposit.");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("You withdrew $" + amount + ". Enjoy!");
} else {
System.out.println("Uh-oh! You don't have enough money or that's not a valid amount to withdraw.");
}
}
public double getBalance() {
return balance;
}
}
In our little story here, we've got a pretend bank account represented by a BankAccount
class. Inside, there's a special variable called balance
that holds how much money is in the account. We put a "private" label on it to keep it safe, like putting a lock on the treasure chest.
To interact with the account, we use methods like deposit()
and withdraw()
. These methods are like the guards who check if it's okay to add or take away money from the account. They make sure everything is done properly and nobody tries anything fishy.
Now, let's see how we use our BankAccount
class:
java
public class EncapsulationExample {
public static void main(String[] args) {
BankAccount myAccount = new BankAccount();
myAccount.deposit(1000);
myAccount.withdraw(500);
System.out.println("Your current balance is $" + myAccount.getBalance() + ". Cool, right?");
}
}
Here, we create a BankAccount
object called myAccount
and play around with it by depositing and withdrawing money. We can't directly access the balance
variable, but we can use the deposit()
, withdraw()
, and getBalance()
methods to interact with it safely.
Read-Only classes
Now, let's add a twist to our tale by creating read-only and write-only classes. These classes will provide limited access to our data, allowing either reading or writing but not both.
java
public class ReadOnlyBankAccount {
private double balance;
public ReadOnlyBankAccount(double initialBalance) {
this.balance = initialBalance;
}
public double getBalance() {
return balance;
}
}
In our ReadOnlyBankAccount class, we only provide a getter method (getBalance()) to retrieve the balance. Once the initial balance is set, it cannot be modified from outside the class, making it read-only.
Write-Only Class
java
public class WriteOnlyBankAccount {
private double balance;
public void setBalance(double newBalance) {
balance = newBalance;
}
}
Conversely, in our WriteOnlyBankAccount class, we only provide a setter method (setBalance()) to modify the balance. Outside callers can only change the balance but cannot read its value, making it write-only.