// Bank Account class demonstrating access modifiers
class BankAccount {
// 1. Private: Sensitive data (only accessible within the class)
private double accountBalance = 1000.00;
private void showBalance() {
System.out.println("Private: Account Balance is $" + accountBalance);
}
// 2. Default: Internal package operations
String accountHolder = "John Doe";
void updateTransactionHistory() {
System.out.println("Default: Transaction history updated for " + accountHolder);
}
// 3. Protected: Shared access for specialized accounts
protected String accountType = "Savings";
protected void calculateInterest() {
System.out.println("Protected: Interest calculated for " + accountType + " account.");
}
// 4. Public: General services
public void displayAccountDetails() {
System.out.println("Public: Account Holder: " + accountHolder);
System.out.println("Public: Account Type: " + accountType);
}
}
// Specialized account class inheriting BankAccount
class PremiumAccount extends BankAccount {
void premiumBenefits() {
System.out.println("Accessing Protected: Premium benefits for " + accountType + " account.");
}
}
// Main Class
public class BankSystem {
public static void main(String[] args) {
BankAccount account = new BankAccount();
// Public: Accessible anywhere
account.displayAccountDetails();
// Default: Accessible within the same package
account.updateTransactionHistory();
// Protected: Accessible within the package or through inheritance
PremiumAccount premium = new PremiumAccount();
premium.calculateInterest();
premium.premiumBenefits();
// Private: Not accessible outside the class
// account.showBalance(); // ERROR: Private access
}
}